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
?
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.
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' ...
There are two aspects of your problem:
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
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.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With