Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get environment variable value in Dockerfile

Tags:

docker

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?

like image 488
Damien MATHIEU Avatar asked Oct 23 '13 09:10

Damien MATHIEU


People also ask

Can Dockerfile read environment variables?

Dockerfile provides a dedicated variable type ENV to create an environment variable. We can access ENV values during the build, as well as once the container runs.

How do I pass an environment variable in Dockerfile?

If you want to pass variables through the docker-compose process into any of the Dockerfiles present within docker-compose. yml , use the --build-arg parameter for each argument to flow into all of the Dockerfiles.

How can I see Docker environment variables?

Fetch Using docker exec Command Here, we are executing the /usr/bin/env utility inside the Docker container. Using this utility, you can view all the environment variables set inside Docker containers.

Can you use an ENV file in a Dockerfile?

You can use docker run --env-file [path-toenv-file] to provide the environment variables to the container from a . env file.


2 Answers

You should use the ARG directive in your Dockerfile which is meant for this purpose.

The ARG instruction defines a variable that users can pass at build-time to the builder with the docker build command using the --build-arg <varname>=<value> flag.

So your Dockerfile will have this line:

ARG request_domain 

or if you'd prefer a default value:

ARG request_domain=127.0.0.1 

Now you can reference this variable inside your Dockerfile:

ENV request_domain=$request_domain 

then you will build your container like so:

$ docker build --build-arg request_domain=mydomain Dockerfile 


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:

[Warning] One or more build-args [foo] were not consumed.

like image 60
Daniel van Flymen Avatar answered Sep 21 '22 10:09

Daniel van Flymen


So you can do: cat Dockerfile | envsubst | docker build -t my-target -

Then have a Dockerfile with something like:

ENV MY_ENV_VAR $MY_ENV_VAR 

I guess there might be a problem with some special characters, but this works for most cases at least.

like image 29
jonasfj Avatar answered Sep 22 '22 10:09

jonasfj