Compose: Relative path to env_file

Created on 26 Apr 2017  路  3Comments  路  Source: docker/compose

I have a problem with my .env file when it's not in the same folder as the docker-compose file. When I place it in a subfolder and reference it, it will simply say the variables are undefined and will default to a blank string. When I remove the .env file it says it can't find the .env file, so it finds the file but doesn't do anything. When I move the .env file to the same directory as the docker-compose file and update it's path or create a symlink instead, all works fine.

Apparently docker-compose has issues with loading env files that are not in the same directory as the docker-compose file.

Currently I "fixed" this by symlinking the .env file as it's shared with a Laravel project in a subfolder.

My docker-compose file (simplified):

version: '3'

services:
  mysql:
    image: mariadb
    volumes:
      - mysql_data:/var/lib/mysql/data
    env_file:
      - ./api/.env
    environment:
      MYSQL_DATABASE: ${DB_DATABASE}
      MYSQL_USER: ${DB_USERNAME}
      MYSQL_PASSWORD: ${DB_PASSWORD}
      MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}

volumes:
  mysql_data:
kinquestion

Most helpful comment

Hey @shin- , i have the exact problem.
I read the official doc(https://docs.docker.com/compose/env-file/), and the topics you posted but that didn't answered my question. As OP said, if the .env file is in a different path than the docker-compose.yml's path it doesn't work.

version: '3.7'
services:
  postgres:
    image: postgres:alpine
    restart: always
    environment:
      POSTGRES_DB: ${DB_NAME}
      POSTGRES_USER: ${DB_USER}
      POSTGRES_PASSWORD: ${DB_PASSWORD}
  php:
    build:
      context: .
      dockerfile: ./docker/php/Dockerfile
    restart: always
    env_file:
      - ./app/.env
    volumes:
      - ./app:/usr/src/app
  nginx:
    image: nginx:1.15.3-alpine
    restart: always
    volumes:
      - ./app:/usr/src/app
      - ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf:ro
    ports:
      - ${NGINX_PORT}:80
    depends_on:
      - php

Inside ./app/ i have my symfony application with its own .env and i want the env vars used for building the images to be put in the same .env, to make it work i have 2 x .env, one in docker-compose dir and one in my app dir

All 3 comments

This has been answered previously:

4001

4618

etc.

See the disambiguation page: https://docs.docker.com/compose/environment-variables/

Hey @shin- , i have the exact problem.
I read the official doc(https://docs.docker.com/compose/env-file/), and the topics you posted but that didn't answered my question. As OP said, if the .env file is in a different path than the docker-compose.yml's path it doesn't work.

version: '3.7'
services:
  postgres:
    image: postgres:alpine
    restart: always
    environment:
      POSTGRES_DB: ${DB_NAME}
      POSTGRES_USER: ${DB_USER}
      POSTGRES_PASSWORD: ${DB_PASSWORD}
  php:
    build:
      context: .
      dockerfile: ./docker/php/Dockerfile
    restart: always
    env_file:
      - ./app/.env
    volumes:
      - ./app:/usr/src/app
  nginx:
    image: nginx:1.15.3-alpine
    restart: always
    volumes:
      - ./app:/usr/src/app
      - ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf:ro
    ports:
      - ${NGINX_PORT}:80
    depends_on:
      - php

Inside ./app/ i have my symfony application with its own .env and i want the env vars used for building the images to be put in the same .env, to make it work i have 2 x .env, one in docker-compose dir and one in my app dir

Afaik the environment variables are imported in the application but you can't use them in your docker-compose file (like you did with NGINX_PORT. Use a separate .env file for that in the same directory and load that from the application if you really really need it.

Was this page helpful?
0 / 5 - 0 ratings