I'm building a container for a ruby app. My app's configuration is contained within environment variables (loaded inside the app with dotenv).
One of those configuration variables is the public ip of the app, which is used internally to make links. I need to add a dnsmasq entry pointing this ip to 127.0.0.1 inside the container, so it can fetch the app's links as if it were not containerized.
I'm therefore trying to set an ENV
in my Dockerfile which would pass an environment variable to the container.
I tried a few things.
ENV REQUEST_DOMAIN $REQUEST_DOMAIN
ENV REQUEST_DOMAIN `REQUEST_DOMAIN`
Everything passes the "REQUEST_DOMAIN" string instead of the value of the environment variable though. Is there a way to pass environment variables values from the host machine to the container?
If you just want to find and replace all environment variables ($ExampleEnvVar) in a Dockerfile then build it this would work:
envsubst < /path/to/Dockerfile | docker build -t myDockerImage . -f -
Load environment variables from a file you create at runtime.
... then in the Dockerfile
where test.sh loads MYVAR from env.sh
add
-e
key for passing environment variables to container. example:So you can do:
cat Dockerfile | envsubst | docker build -t my-target -
Then have a Dockerfile with something like:
I guess there might be a problem with some special characters, but this works for most cases at least.
An alternative using
envsubst
without losing the ability to use commands likeCOPY
orADD
, and without using intermediate files would be to use Bash's Process Substitution:NOTE: Originally wanted to add a comment to jonasfj's answer but it seems I don't have enough reputation.
You should use the
ARG
directive in your Dockerfile which is meant for this purpose.So your Dockerfile will have this line:
or if you'd prefer a default value:
Now you can reference this variable inside your Dockerfile:
then you will build your container like so:
Note 1: Your image will not build if you have referenced an
ARG
in your Dockerfile but excluded it in--build-arg
.Note 2: If a user specifies a build argument that was not defined in the Dockerfile, the build outputs a warning: