I know that I can use ENV
to set environment variables one by one in my Dockerfile.
My question is that, is there a way (other than perhaps adding some script to bashrc in the image) to set the environment variables inside the Dockerfile from a .env file?
Note: In case this is not clear enough, I’m not talking about passing variables in docker run
, I want the container to run with environment variables already set.
2
Answers
Rather than specifying single environment variables on the
docker run
command one by one, you can use--env-file
to specify lots of them via a file.If you want to push all currently set variables into the container at runtime, use a combination of
If you need the same at container build time, maybe have a command in Dockerfile to copy env.txt into the container and source it from within.
Now as you mention the container is not run locally, you can still set environment variables at runtime using mechanisms like Kubernetes ConfigMaps.
You can do few other things:
/etc/profile.d/
docker run --env-file envfile