Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use environment variables in .env file in docker-compose.yml?

Tags:

docker

I am trying to use an environment variable in the docker-compose.yml file. I have a file my-great-env.env.

Here is how they look:

docker-compose.yml

version: '3.4'

services:
    blahblah:
        images: greatimage
        volumes:
          - "${MY_PATH}:c:\\FinalFolder"

my-great-env.env

MY_PATH=C:\the\path\to\folder

When I try to docker run this, I get

The MY_PATH variable is not set. Defaulting to a blank string.

How do I use the environment variables defined in the .env file in docker-compose.yml?

like image 425
noblerare Avatar asked Jul 25 '18 20:07

noblerare


People also ask

Can I use environment variables in Docker compose file?

Docker Compose allows us to pass environment variables in via command line or to define them in our shell. However, it's best to keep these values inside the actual Compose file and out of the command line.

How do I pass environment variables to Docker containers?

With a Command Line Argument The command used to launch Docker containers, docker run , accepts ENV variables as arguments. Simply run it with the -e flag, shorthand for --env , and pass in the key=value pair: sudo docker run -e POSTGRES_USER='postgres' -e POSTGRES_PASSWORD='password' ...


1 Answers

There are two aspects of your problem:

  1. if you want to use the environment vars in docker-compose.yml file, you have to put them in file named .env as stated here: https://docs.docker.com/compose/environment-variables/#the-env-file
  2. you can use the env_file configuration option to use any file with variables definitions as described here: https://docs.docker.com/compose/environment-variables/#the-env_file-configuration-option BUT! they will be visible only in containers, not in the docker-compose.yml file.
like image 137
Izydorr Avatar answered Sep 18 '22 23:09

Izydorr