I’ve seen this being asked quite a bit, so I’ve decided to write this answer outlining a few ways how to pass environment variables to a Docker container.
Hope that this is helpful! If you have any questions post the below!
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
Set environment variable with the
-e
flagThe
-e
flag can be used to pass environment variables to a Docker container.For example, if you want to pass the environment variable
MY_ENV_VAR
to the container, you can do the following:The
-e
is short for--env
, so you can also use that instead:The
-e
flag can be used multiple times to pass multiple environment variables:The same can be achieved with
docker-compose
:Set environment variable with the
--env-file
flagIf you want to pass a file containing all of your environment variables to a Docker container, you can use the
--env-file
flag.The
--env-file
flag allows you to pass a file containing environment variables to a Docker container.Let’s say that you have a
.env
file containing the following environment variables:Rather than passing the environment variables directly to the container, you can pass the
.env
file to the container using the--env-file
flag:Alternatively, if you are using
docker-compose
, you can do the same thing:Set environment variable within
docker-compose.yml
If you are using
docker-compose
to manage your Docker containers, you can pass environment variables to the container using theenvironment
key in thedocker-compose.yml
file.For example:
For more information, see the Docker Compose documentation.
Hope that this helps!