0

I know that docker compose can read environment variables into itself by reading .env file that must reside in the same directory where the docker-compose file is located. Usually this is the root of the project.

I want to keep ALL my environment files in a specific dedicated directory called envs.

So I have this setup:

\project_root:
    \envs
        .compose_envs.yml
        .django_envs.yml
    main.py
    docker-compose.yml

Is there a directive for docker compose to read environment variables from this custom location?

PS: I know I can do something like this:

set -a; source /path/to/your/env/file; set +a; docker-compose up -d

but I would like to use a docker compose directive and run the container just with docker compose up

PPS: this question is not about -env_file directive, since this one populates the container with the specified variables, but I want to reference environment variables in the docker-compose itself, for example in this section:

environment:
      POSTGRES_DB: ${POSTGRES_DB}

Some good solutions here:
How can I use environment variables in docker-compose?
but still nothing about my use case

ruslaniv
  • 458
  • 1
  • 6
  • 14
  • `question is not about -env_file directive, since this one populates the container with the specified variables` no? just `docker-compose --env-file envs/yourenv up` – KamilCuk Jul 21 '23 at 09:33
  • @KamilCuk Yes, I will edit the question, since I really want to just run `docker compose up` without any arguments – ruslaniv Jul 21 '23 at 09:37
  • 1
    `docker-compose() { command docker-compose --env-file "$MY_ENV_FILE" "$@"; }; MY_ENV_FILE=/here/and/there; docker-compose up`. You can throw abstractions at it. Wrap it in a custom script. Create a symlink `.env -> envs/env`. Finally, you can patch docker compose to support a DOCKER_COMPOSE_ENV_FILE environment variable. – KamilCuk Jul 21 '23 at 09:42
  • Yes, symlinking sounds like an acceptable solution, I'm going to give it a try! – ruslaniv Jul 21 '23 at 09:55

0 Answers0