[BUG] docker compose only tries to load the .env
located near the first docker-compose.yml
file even if it not in the PWD
#12054
Labels
Description
Hi, I have a project with a structure like this:
. ├── .env ├── docker-compose.yml ├── front │ └── docker-compose.yml └── api └── docker-compose.yml
My goal is to be able to start both the front and the back sub-projects in one command. To do so, I'm using a
docker-compose.yml
file in the project root which adds the dependencies between the services of the front and the back.However, when I run the command
docker compose -f api/docker-compose.yml -f front/docker-compose.yml -f docker-compose.yml up
in the project root to start everything, it crashes with the error :error while interpolating services.api.environment.[]: required variable POSTGRES_USER is missing a value: Missing username
It is really weird because this environment variable is defined in the
.env
file. After some trial and error, I found that using the very similar commanddocker compose -f docker-compose.yml -f api/docker-compose.yml -f front/docker-compose.yml up
works. I have also tried a few other commands and I have concluded that docker compose only tries to read the.env
file at the level of the firstdocker-compose.yml
file. You could tell me that I could just change the order of the files so that I don't get the error anymore, except that by changing the order, the files are not combined in the same way. In my case, I would have liked thename
of the docker compose project to be the one of the project rootdocker-compose.yml
file and not the ones of the other files so I need it to be the last file.To summarise, here's what's happening:
docker compose -f api/docker-compose.yml -f front/docker-compose.yml -f docker-compose.yml up
in project root : Don't workdocker compose -f docker-compose.yml -f api/docker-compose.yml -f front/docker-compose.yml up
in project root : Works.env
file in theapi
directory and runningdocker compose -f api/docker-compose.yml -f front/docker-compose.yml -f docker-compose.yml up
in project root : Worksdocker compose -f api/docker-compose.yml -f front/docker-compose.yml -f docker-compose.yml --env-file .env up
in project root : WorksThese behaviours go quite against what is written in the documentation, where it is said that the
.env
file is loaded from the local working directory (PWD) which is the project root in these cases. This is also counter-intuitive, as by manually specifying the.env
from the local working directory (PWD) in the first command, it works (as shown in the last example).I also tested this on an old version of docker compose that I had (v2.11.0) and the bug already existed so the problem doesn't seem to come from the changes that have been made this year concerning the
.env
files.Steps To Reproduce
1. In this environment:
Please see section
Compose version
andDocker environment
2. With this config:
.env
docker-compose.yml
front/docker-compose.yml
back/docker-compose.yml
3. Run
4. See error
error while interpolating services.api.environment.[]: required variable POSTGRES_USER is missing a value: Missing username
Compose Version
Docker Environment
Anything else?
No response
The text was updated successfully, but these errors were encountered: