Today I try to dockerize an full app ( frontend , API and a database ) so that I could deploy it on a local machine or/and a virtual machine.
When doing :
docker-compose up
I got an "Connect ECONNREFUSED" when I try to start my API that will connect to the database (it uses Sequelize under the hood) with the following env variable :
DATABASE_URL: "postgresql://${POSTGRES_USER:-postgres}:${POSTGRES_PASSWORD:-jy95}@db:5432/${POSTGRES_DB:-sourcecode}"
In docker docs, it is said :
Each container can now look up the hostname web or db and get back the appropriate container’s IP address. For example, web’s application code could connect to the URL postgres://db:5432 and start using the Postgres database.
In PostgreSQL docs, we can see that postgresql:// is also valid :
The URI scheme designator can be either postgresql:// or postgres://. Each of the URI parts is optional.
Here is my full docker-compose.yml file :
version: '3.7'
services:
frontend:
image: jy95/sourcecode-front
restart: always
depends_on:
- api
ports:
- "80:3000"
- "443:3000"
environment:
API_SERVER: "api:8080"
CDN_SERVER: "api:8080/files"
api:
image: jy95/sourcecode_api
restart: always
ports:
- "8080:3000"
depends_on:
- db
environment:
DATABASE_URL: "postgresql://${POSTGRES_USER:-postgres}:${POSTGRES_PASSWORD:-jy95}@db:5432/${POSTGRES_DB:-sourcecode}"
# use a env file if you want to use other values that default ones
# https://docs.docker.com/compose/environment-variables/#the-env_file-configuration-option
db:
image: postgres:12-alpine
restart: always
ports:
- "5432:5432"
environment:
# use a env file for that part later
# https://docs.docker.com/compose/environment-variables/#the-env_file-configuration-option
POSTGRES_USER: postgres
POSTGRES_PASSWORD: jy95
POSTGRES_DB: sourcecode
POSTGRES_PORT: 5432
# If we want to access more easily to the database (not recommended)
# PGDATA: /var/lib/postgresql/data/pg_data
#volumes:
# - pg_data:/var/lib/postgresql/data/pg_data
Thanks in advance for the help
PS: The images are already on the Docker Hub
${POSTGRES_PASSWORD:-jy95}) like this oneDATABASE_URL=pgsql://test:test@postgresql:5432/test_db. You could export it in your terminal to testexport DATABASE_URL....docker-compose up, with no other options, and watch the error messages go by; are there database messages that come out after your application connect error?