Search code examples
dockerdocker-composeenvironment-variables

Docker compose file is not using .env variables


I have an .env file in my root directory and in my sub directory a docker compose file.

No what I want to do is to use the variables from my .env file for port forwaring, which is currently only working if the .env file is in the same directory as the docker compose file.

services:
  postgres:
    container_name: postgres
    image: postgres:15.4
    ports:
      - ${DB_PORT}:5432
    environment:
      POSTGRES_DB: postgres
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password
      PGUSER: user

The porject structure:

/project-root
  ├── folder
     ├── sub folder
       ├── .env
       ├── docker-compose.yml

If I specify the location, the enviornment variables are found by the container and I can find them in the config, but the forwarding is not working:

services:
  postgres:
    container_name: postgres
    image: postgres:15.4
    env_file:
      - ../../.env
    ports:
      - ${DB_PORT}:5432
    environment:
      POSTGRES_DB: postgres
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password
      PGUSER: user

The porject structure:

/project-root
  ├── .env (the env file in the root)
  ├── folder
     ├── sub folder
       ├── docker-compose.yml

This will use a random port (like 32770, 32771..). But I see that the env is available in my container. enter image description here

If I run it with

    ports:
      - ${DB_PORT:?error}:5432

An error will be thrown. error while interpolating services.postgres.ports.[]: required variable DB_PORT is missing a value: error

The .env file:

DB_PORT=5433

To start the services I'm using

docker compose up

Anyone an idea why?


Solution

  • Compose has two different layers of environment variable interpolation. env_file: affects environment variables set for the container, but it doesn't affect variable substitution in the Compose file itself. Normally the .env file does need to be in the same directory as the docker-compose.yml file.

    Compose variable interpolation takes its values from a couple of sources: the host's environment (e.g., export commands you've run in the same shell as docker-compose), the docker-compose --env-file file, or a .env file in the same directory as the Compose file. You can't specify a different location for this in the Compose file itself.

    The easiest approach here is to use the first file structure you show in the question, and move the .env file next to the docker-compose.yml file. There is also a docker-compose --env-file option you can use if you want to put the file somewhere else, but you need to specify it every time you run a Compose command.

    # easier
    cd 'project-root/folder/sub folder'
    git mv ../../.env .
    git commit -m 'moved .env to correct location'
    docker-compose up -d
    docker-compose port postgres 5432
    
    # harder
    cd 'project-root/folder/sub folder'
    docker-compose --env-file ../../.env up -d
    docker-compose --env-file ../../.env port postgres 5432
    

    env_file: only affects the environment variables set for a specific container, and it takes effect after variable substitution on the Compose file takes effect. Each container can have a different env_file:. If you docker-compose run postgres env with the latter Compose configuration, you'd see the variables like $DB_PORT set in the container's environment, but that's separate from the environment used for variable substitution in the Compose file.