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.

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?

4

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật