5

I am trying to run docker-compose up with the following configuration:

php:
    image: php:7.0-fpm
    expose:
        - 9000
    links:
        - nginx

nginx:
    env_file: .env
    image: nginx:latest
    ports:
        - 80:80
        - 433:433
    environment:
        - NGINX_HOST: ${APP_URL}

mysql:
    env_file: .env
    image: mysql:latest
    ports:
        - 3306:3306
    environment:
        - MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
        - MYSQL_DATABASE: ${DB_DATABASE}
        - MYSQL_USER: ${DB_USERNAME}
        - MYSQL_PASSWORD: ${DB_PASSWORD}
        - MYSQL_ALLOW_EMPTY_PASSWORD: no

I have an .env file in the same directory and am able to test the variable in the shell, but docker doesn't seem to load the .env.

WARNING: The APP_URL variable is not set. Defaulting to a blank string.

WARNING: The DB_PASSWORD variable is not set. Defaulting to a blank string.

WARNING: The DB_DATABASE variable is not set. Defaulting to a blank string.

WARNING: The DB_USERNAME variable is not set. Defaulting to a blank string.

ERROR: Validation failed in file './docker-compose.yaml'


UPDATE

I just changed the env_file value to point to a non-existing file and no errors are thrown. It seems like docker is completely ignoring the option.

Dov Benyomin Sohacheski
  • 7,133
  • 7
  • 38
  • 64
  • You can always check if the docker-compose file is set up as you expected with: `docker-compose convert` Then you're able to see the actual settings (no need to trial and error). – Pedram Nov 23 '22 at 07:51
  • I have the same issue using the same configuration file (except I'm using MariaDB instead of MySQL). It seems like PHP & MySQL are receiving the environment correctly whereas NGINX is not. I have to manually provide the environment variable to the `nginx` service by using the `environment` key. – Amin NAIRI Feb 03 '23 at 10:36

2 Answers2

3

Like many other version related issues, updating to v1.7.1 of docker-compose resolved the issue, works like a charm!

Dov Benyomin Sohacheski
  • 7,133
  • 7
  • 38
  • 64
  • 3
    How do you update docker-compose? I have the latest version of Docker 17.06.0 and docker-compose is 1.14.1 – ChrisRich Aug 14 '17 at 11:20
  • I also have compose in 1.14.0 and docker in 17.05 but can't get environment variables work in docker-compose file. – Lion Feb 04 '18 at 12:05
2

I got this issue because I wasn't running $docker-compose up from the same directory as my .env and docker-compose.yml file. The command can still find the docker-compose.yml file, through a search, but it doesn't also search that location for the .env file.

mikeLundquist
  • 769
  • 1
  • 12
  • 26