Get environment variable value in Dockerfile

Docker

Docker Problem Overview


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?

Docker Solutions


Solution 1 - Docker

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.

Solution 2 - Docker

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.

Solution 3 - Docker

An alternative using envsubst without losing the ability to use commands like COPY or ADD, and without using intermediate files would be to use Bash's [Process Substitution][1]:

docker build -f <(envsubst < Dockerfile) -t my-target .

[1]: http://tldp.org/LDP/abs/html/process-sub.html "Process Sustitution" [2]: https://stackoverflow.com/a/21388443/4735682

Solution 4 - Docker

This is for those looking to pass env variable from docker-compose using .env file to dockerfile during build and then pass those args as env variable to container. Typical docker-compose file

services:
  web:
    build:
      context: ./api
      dockerfile: Dockerfile
      args:
        - SECRET_KEY=$SECRET_KEY
        - DATABASE_URL=$DATABASE_URL
        - AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID

Pass the env variable present in .env file to args in build command. Typical .env file

SECRET_KEY=blahblah
DATABASE_URL=dburl

Now when you run docker-compose up -d command, docker-compose file takes values from .env file then pass it to docker-compose file. Now Dockerfile of web containes all those varibales through args during build. Now typical dockerfile of web,

FROM python:3.6-alpine

ARG SECRET_KEY
ARG DATABASE_URL
ARG AWS_ACCESS_KEY_ID
ARG AWS_SECRET_ACCESS_KEY
ARG AWS_BUCKET
ARG AWS_REGION
ARG CLOUDFRONT_DOMAIN

ENV CELERY_BROKER_URL redis://redis:6379/0
ENV CELERY_RESULT_BACKEND redis://redis:6379/0
ENV C_FORCE_ROOT true
ENV SECRET_KEY  ${SECRET_KEY?secretkeynotset}
ENV DATABASE_URL ${DATABASE_URL?envdberror}

Now we recieved those secret_key and db url as arg in dokcerfile. Now let's use those in ENV as ENV SECRET_KEY ${SECRET_KEY?secretkeynotset}. Now even docker container has those variables in it's environment. Remember not to use ARG $SECRET_KEY(which I did). It should be ARG SECRET_KEY

Solution 5 - Docker

Load environment variables from a file you create at runtime.

export MYVAR="my_var_outside"
cat > build/env.sh <<EOF
MYVAR=${MYVAR}
EOF

... then in the Dockerfile

ADD build /build
RUN /build/test.sh

where test.sh loads MYVAR from env.sh

#!/bin/bash
. /build/env.sh
echo $MYVAR > /tmp/testfile

Solution 6 - Docker

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 -

Solution 7 - Docker

add -e key for passing environment variables to container. example:

$ MYSQLHOSTIP=$(sudo docker inspect -format="{{ .NetworkSettings.IPAddress }}" $MYSQL_CONRAINER_ID)
$ sudo docker run -e DBIP=$MYSQLHOSTIP -i -t myimage /bin/bash

root@87f235949a13:/# echo $DBIP
172.17.0.2

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDamien MATHIEUView Question on Stackoverflow
Solution 1 - DockerDaniel van FlymenView Answer on Stackoverflow
Solution 2 - DockerjonasfjView Answer on Stackoverflow
Solution 3 - DockerL. Alberto GiménezView Answer on Stackoverflow
Solution 4 - DockerZincfanView Answer on Stackoverflow
Solution 5 - DockerDan CView Answer on Stackoverflow
Solution 6 - DockersnassrView Answer on Stackoverflow
Solution 7 - DockerValentin KantorView Answer on Stackoverflow