compose icon indicating copy to clipboard operation
compose copied to clipboard

env_file is ignored and not validated when no .env is present

Open tze opened this issue 10 years ago • 2 comments

According to the documentation for env_file, a file relative to the referencing docker-compose.yml should be read by compose. However it looks like env_file is ignored completely until a .env file is present. When .env is present, the referenced env_file existence is validated, but the contents of the referenced env_file are still being ignored.

Docker for windows docker-compose version 1.8.1, build 004ddae Docker version 1.12.3, build 6b644ec

# docker-compose.yml
version: '2'
services:    
    db:
        env_file: conf.env
        image: mariadb
        ports:
            - "$DB_PORT:3306"
        environment:
            - MYSQL_ROOT_PASSWORD=$DB_MYSQL_ROOT_PASSWORD
# conf.env    
DB_PORT=13306
DB_MYSQL_ROOT_PASSWORD=the-root-password
$ ls -la
total 14
drwxr-xr-x 1    0 Nov 10 16:40 ./
drwxr-xr-x 1    0 Nov 10 16:39 ../
-rw-r--r-- 1   55 Nov 10 16:40 conf.env
-rw-r--r-- 1  216 Nov 10 16:40 docker-compose.yml

$ docker-compose.exe -p envfile up
The DB_MYSQL_ROOT_PASSWORD variable is not set. Defaulting to a blank string.
The DB_PORT variable is not set. Defaulting to a blank string.
The Compose file '.\docker-compose.yml' is invalid because:
services.db.ports is invalid: Invalid port ":3306", should be [[remote_ip:]remote_port[-remote_port]:]port[/protocol]

When changing env_file: conf.env to env_file: THERE-IS-NO-conf.env there is no error:

$ docker-compose.exe -p envfile up
The DB_MYSQL_ROOT_PASSWORD variable is not set. Defaulting to a blank string.
The DB_PORT variable is not set. Defaulting to a blank string.
The Compose file '.\docker-compose.yml' is invalid because:
services.db.ports is invalid: Invalid port ":3306", should be [[remote_ip:]remote_port[-remote_port]:]port[/protocol]

But when a .env file is present, env_file is at least validated correctly (still env_file: THERE-IS-NO-conf.env)

$ cp conf.env .env
$ docker-compose.exe -p envfile up
Couldn't find env file: C:\tmp\docker\THERE-IS-NO-conf.env

Reverting back to env_file: conf.env appears to be working, since the file exists, but the contents of conf.env are still ignored and read from .env instead.

tze avatar Nov 10 '16 16:11 tze

The .env file and the env_file key are two different things. Please see the disambiguation page for more information.

shin- avatar Nov 10 '16 20:11 shin-

Ok, so it is an usage error on my side. The missing environment variable errors were hiding the expected missing env_file error. Thanks for clearing that up, shin-. By looking at other bug reports and some StackOverflow questions I would say the environment variable subject leads to confusion quite often. Maybe link the disambiguation page from the general compose-file page to foolproof things up?

tze avatar Nov 11 '16 08:11 tze