Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relative path to env_file #4764

Closed
mdeboer opened this issue Apr 26, 2017 · 7 comments
Closed

Relative path to env_file #4764

mdeboer opened this issue Apr 26, 2017 · 7 comments

Comments

@mdeboer
Copy link

mdeboer commented Apr 26, 2017

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:
@shin-
Copy link

shin- commented Apr 26, 2017

This has been answered previously:
#4001
#4618
etc.

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

@lebadapetru
Copy link

lebadapetru commented May 2, 2019

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

@mdeboer
Copy link
Author

mdeboer commented Oct 17, 2019

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.

@syerad
Copy link

syerad commented Feb 17, 2021

Is there an update regarding that issue?
Afaik it's still not possible to set a relative path in the env_file option.

@nk9
Copy link

nk9 commented Jul 24, 2021

Relative paths are accepted in env_file, they are even used in the documentation. Note that PWD is the directory that the docker-compose file is in, if you are running it from the command line with -f. And, of course, you can load the env_file just fine and still not be able to access the variables it declares elsewhere in the docker-compose file for the reasons discussed above.

@jxramos
Copy link

jxramos commented Jun 16, 2022

So in the yml file env_file is a relative path rooted at the location of the yml file and must navigate to wherever the file is in the filesystem

So if given the following file locations

projX/subdir/docker-compose.yml
envs/my_vars.env

Then we'd have

  foooo:
    env_file:
      - ./../../envs/my_vars.env

How is the behavior when using --env-file at the command line? Would one do

# relative to cwd
docker-compose -f projX/subdir/docker-compose.yml --env-file ./envs/my_vars.env up

or rather

# relative to yml
docker-compose -f projX/subdir/docker-compose.yml --env-file ./../../envs/my_vars.env

?

@mysterious-ben
Copy link

While this behavior is indeed specified in the docs, it could have been highlighted because it is a common reason for confusion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants