Compose: docker-compose file version 3 not reading ENV files properly, is this a bug?

Created on 26 Mar 2017  路  8Comments  路  Source: docker/compose

I have the following definition in my docker-compose.yml file:

    version: '3'
    services:
        mysqldb:
            image: mysql:5.6
            healthcheck:
                test: "exit 0"
            env_file: .env_mysql
            environment:
                MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
                MYSQL_DATABASE: ${MYSQL_DATABASE}
                MYSQL_USER: ${MYSQL_USER}
                MYSQL_PASSWORD: ${MYSQL_PASSWORD}
            volumes:
                - ./dump:/docker-entrypoint-initdb.d

And this is how the file .env_mysql looks like (values are just examples):

    MYSQL_ROOT_PASSWORD=pass1
    MYSQL_DATABASE=db1
    MYSQL_USER=user1
    MYSQL_PASSWORD=pass2

This is the result of run the command docker-compose up:

    > docker-compose up
    WARNING: The MYSQL_ROOT_PASSWORD variable is not set. Defaulting to a blank string.
    WARNING: The MYSQL_PASSWORD variable is not set. Defaulting to a blank string.
    WARNING: The MYSQL_USER variable is not set. Defaulting to a blank string.
    WARNING: The MYSQL_DATABASE variable is not set. Defaulting to a blank string.
    Creating network "dockeramp_default" with the default driver
    Pulling mysqldb (mysql:5.6)...
    ...
    Status: Downloaded newer image for mysql:5.6
    Creating dockeramp_mysqldb_1
    Attaching to dockeramp_mysqldb_1
    mysqldb_1  | error: database is uninitialized and password option is not specified
    mysqldb_1  |   You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD
    dockeramp_mysqldb_1 exited with code 1

Besides the value I have in the example above I have tried this other combinations of file name:

  • .env-mysql => didn't work
  • mysql.env => didn't work

Only .env is accepted which is the opposite said here. Is this a bug? It's intended to be like it's? Can any test this and see if it's a problem on my side or a common problem from docker-compose?

As an addition I am running Docker for Windows and I have the following version of Docker and Docker Compose respectively installed:

> docker-compose -v
docker-compose version 1.11.2, build f963d76f

> docker -v
Docker version 17.03.1-ce-rc1, build 3476dbf
kinquestion

Most helpful comment

Hello there,
Any news for chancing to use other name ( eg nginx.env, db.env) instead only .env.
@nitinprakash96 I followed your link but the author also said:
The docs say you can specify you're own env-file or even multiple files, however I could not get that working. It always wanted to choose the .env file.
I also tried to use double-dollar sign but still doesn't working.
Thanks in advance <3.

All 8 comments

the .env file's vars are available at docker-compose stage whereas all other env vars set via env_file (.env_mysql in your case) or env directive are only available within the container.

You need to escape the variable if you want it to be expanded inside the container, using a double-dollar sign.

Perhaps, you can here to http://staxmanade.com/2016/05/how-to-get-environment-variables-passed-through-docker-compose-to-the-containers/

A cursory search through previous issues would have answered that question, which has been asked many times now: https://github.com/docker/compose/issues/4189#issuecomment-274915631

Hello there,
Any news for chancing to use other name ( eg nginx.env, db.env) instead only .env.
@nitinprakash96 I followed your link but the author also said:
The docs say you can specify you're own env-file or even multiple files, however I could not get that working. It always wanted to choose the .env file.
I also tried to use double-dollar sign but still doesn't working.
Thanks in advance <3.

@reypm Is this solved for you? I got stuck with the same issue. Please help

I made a thing to sort out all the environment variable craziness of docker-compose and I think your problem is that variable expansion doesn't use env_file. Here's some options which I think will work, though:

Best option IMHO

Don't declare the variables in environment block. Only declare them in the file referenced by env_file. In your example, I'd just get rid of the environment block entirely so your docker-compose.yml would look like this:

version: '3'
services:
    mysqldb:
        image: mysql:5.6
        healthcheck:
            test: "exit 0"
        env_file:
          - .env_mysql
        volumes:
            - ./dump:/docker-entrypoint-initdb.d

This is also a fine option.

Use command line variables.

docker-compose run -e MYSQL_ROOT_PASSWORD="pass1" -e MYSQL_DATABASE="db1" ... mysqldb

This will make it difficult to have multiple instance configurations in the same stack.

Use .env.

$ mv .env_mysql .env
$ docker-compose run mysqldb

Probably not good advice to put your container configuration in your host configuration.

Define the variables in your host environment.

$ export MYSQL_ROOT_PASSWORD="pass1"
$ export MYSQL_DATABASE="db1"
...
$ docker-compose run mysqldb
  env_file:
     - .env_mysql

env_file expects a list of files

env_file:
- .env_mysql
env_file expects a list of files

@matthias-p-nowak For a single file, I think it can be either a string or a list.

I declare the variables in docker-compose and Dockerfile, then worked.

docker-compose
env_file: .env

Dockerfile
RUN ENV=${ENV}

Was this page helpful?
0 / 5 - 0 ratings