0

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?

1
  • I don't believe the dollar-curly brace syntax works in the docker compose yaml file, but the way you have your .env file should suffice. And it looks like the variables you have defined in your environment block are overwriting what you have in the .env unless I am missing something Commented Jun 14 at 1:59

1 Answer 1

0

So I figure it out.

Not 100%, but based on what I observed, when env_file is used, it will automatically load all the env variables and match the key to the environment,

Meaning:

POSTGRES_USER
POSTGRES_PASSWORD
etc

What I should do instead is just remove the entire environment block instead:

  postgresdb:
    image: postgres:12.0-alpine
    container_name: postgresdb
    ports:
      - "5432:5432"
    volumes:
      - ./.data/postgres_data:/var/lib/postgresql/data/
    networks:
      - demoapp
    env_file:
      - ./app/.env
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.