Tal Rofe
Tal Rofe

Reputation: 1844

docker compose wont populate env variables into script

I try to use some environment variable inside my docker compose file:

backend:
    container_name: backend
    build:
        context: ./docker
        dockerfile: Dockerfile.api-dev
        args:
            - API_NAME=backend
    env_file:
        - ./apps/backend/envs/.env.development
        - ./docker/envs/.env.development
    ports:
        - ${PORT}:${PORT}
    restart: always
    depends_on:
        - mongo_replica_1
        - mongo_replica_2
        - mongo_replica_3
    networks:
        - mongo_network

As you can see I provide env_file. There are 2 files. One of them has PORT configured. But when I run the docker-compose up I get a warning: WARN[0000] The "PORT" variable is not set. Defaulting to a blank string.

So where is the problem?

Upvotes: 0

Views: 94

Answers (1)

Mihai
Mihai

Reputation: 10737

This part:

    env_file:
        - ./apps/backend/envs/.env.development
        - ./docker/envs/.env.development

defines environment variables for the RUNNING service/container.

The port that you want to substitute is read by docker-compose at config time (when it interprets the docker-compose.yml and tries to understand what you need).

For that you need a .env next to the docker-compose.yml and define the PORT in the .env. Or if your environment file has a different name:

docker-compose --env-file .env.development up

Upvotes: 1

Related Questions