env_file does not allow /dev/null
docker-compose.yml does not allow env_file: /dev/null, because it tests the provided path with os.path.isfile, and /dev/null is not a regular file. This breaks the following use case:
env_file:
- defaults.env
- ${EXTRA_ENV_FILE:-/dev/null}
which, if not for the overly strict check, would allow EXTRA_ENV_FILE=extra.env docker-compose up, while also letting the user run the service without overriding the environment. There is a workaround of adding a blank file to version control and using that instead, but it’s clumsy.
I suggest that env_file: /dev/null be permitted, either by allowing any path that exists, or by special-casing /dev/null (so it also works on Windows).
Engine do support passing --env-file /dev/null so compose should do the same
You can use a .env file instead.
Engine do support passing --env-file /dev/null so compose should do the same
@ndeloof I'm aware this issue is old and closed but I'm not sure of the accuracy of this statement.
antonio@ark:~$ docker --version
Docker version 20.10.6, build 370c289
antonio@ark:~$ docker run --rm --env-file=/dev/null alpine:latest sh -c 'echo $FOO'
antonio@ark:~$ docker run --rm --env-file=blah alpine:latest sh -c 'echo $FOO'
i am defined
At the time this issue was submitted, docker engine would have been 19.03 so:
antonioabella@swarm-test:~$ docker --version
Docker version 19.03.10, build 9424aeaee9
antonioabella@swarm-test:~$ docker run --rm --env-file=/dev/null alpine:latest sh -c 'echo $FOO'
antonioabella@swarm-test:~$ docker run --rm --env-file=bla alpine:latest sh -c 'echo $FOO'
hi
Engine seems to handle --env-file=/dev/null just fine in both versions. This appears to be a failing of compose, not engine.
@a-abella yes, I fully agree. As I said "docker run does, so compose should do the same"
As the reported use-case was to be able to pass extra environment file, @ulyssessouza suggestion to use a .env file or --env-file option is relevant, still docker-compose should not FAIL when env file is set to /dev/null
@ndeloof Oh, I misread your comment. my mistake
Can this be reopen? This issue has been closed without addressing the problem
@ulyssessouza
My bad: as I tested on Mac OS, obviously --env-file=/dev/null worked, but I eventually got bootcamp setup with Docker Desktop Windows so I can give it a try on Microsoft OS, and :
Microsoft Windows [version 10.0.19045.2251]
(c) Microsoft Corporation. Tous droits réservés.
C:\Users\nicolas>docker run --env-file /dev/null alpine
docker: open /dev/null: Le chemin d’accès spécifié est introuvable.
See 'docker run --help'.
So, for sure we can do some magic in Docker Compose, but we should not implement things that the docker/cli doesn't support.
Alternatively, considering your initial feature request, we could manage empty values set in env_file list, i.e
env_file:
- defaults.env
- ${EXTRA_ENV_FILE}
when EXTRA_ENV_FILE si not set, we could ignore empty entry in the list and only load defaults.env
Closing this issue in favor for https://github.com/docker/compose/issues/9181