I have the following docker-compose.yml file
version: "3.9"
services:
api:
build:
context: .
dockerfile: ./.docker/local/django/Dockerfile
container_name: app
ports:
- "8000:8000"
env_file:
- ./app/.env
depends_on:
- postgresdb
- redis
postgresdb:
image: postgres:12.0-alpine
container_name: postgresdb
ports:
- "5432:5432"
volumes:
- ./.data/postgres_data:/var/lib/postgresql/data/
environment:
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
- POSTGRES_DB=${POSTGRES_DB}
networks:
- demoapp
env_file:
- ./app/.env
With .env file in /app/.env where the application resides
POSTGRES_ENGINE=django.db.backends.postgresql
POSTGRES_USER=admin
POSTGRES_PASSWORD=admin123456
POSTGRES_DB=demoapp
PG_HOST=postgresdb
PG_PORT=5432
However, I can't seem to load the .env variables to the container, thus not creating the database in Postgres.
Below is the docker-compose config result
networks:
demoapp:
driver: bridge
services:
api:
...
environment:
...
PG_HOST: postgresdb
PG_PORT: '5432'
POSTGRES_DB: demoapp
POSTGRES_ENGINE: django.db.backends.postgresql
POSTGRES_PASSWORD: admin123456
POSTGRES_USER: admin
postgresdb:
...
container_name: postgresdb
environment:
...
PG_HOST: postgresdb
PG_PORT: '5432'
POSTGRES_DB: '' # was added in the docker-compose
POSTGRES_ENGINE: django.db.backends.postgresql
POSTGRES_PASSWORD: '' # was added in the docker-compose
POSTGRES_USER: '' # was added in the docker-compose
version: '3.9'
Based on the config result, all the environment that I declared in my docker-compose file becomes and empty string.
Tested the same in the api (loaded the same environment to api and it will result to an empty string too)
What should I do so that the env variables are loaded to the Postgres?