How to get an environment variable value into Dockerfile during "docker build"?
Asked Answered
S

10

389

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?

Sisely answered 23/10, 2013 at 9:16 Comment(0)
C
586

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.

Cote answered 4/1, 2016 at 21:23 Comment(10)
ARG is available from docker v1.9 onwards.Roselynroseman
is this supported to be built remotely on docker repo?Frei
Your image will not build if you have referenced an ARG in your Dockerfile but excluded it in --build-arg you are wrong. You can build image with references even without --build-arg. Moreover you can set default value for the build arg.Lammergeier
What is the purpose of the ENV line? For me --build-arg + ARG was enough.Omnibus
The ARG defines a variable to be used inside the dockerfile in subsequent commands. The ENV defines an environment variable which is passed to the container.Campobello
Why is there = in ENV request_domain=$request_domain? I thought the syntax would be like ENV request_domain $request_domain. BTW, correct me if I am wrong, but why doesn't ENV request_domain=$request_domain (or ENV request_domain $request_domain) just work? Isn't it passing the value of request_domain to docker?Crozier
This will not work for AWS_REGION environment variableUnexpressed
Please note that while on both Windows and Linux, the Dockerfile itself will expect variables with the $var syntax... When using RUN commands on Windows, cmd will still expect the %var% syntax and powershell will still expect the $env:var syntax.Herlindaherm
I think you want to point the build command at a directory, like .. Not Dockerfile. At least, that's what worked for me, and is what is described here: https://mcmap.net/q/87988/-docker-build-gives-quot-unable-to-prepare-context-context-must-be-a-directory-users-tempuser-git-docker-dockerfile-quotSergei
#72217266 Please help me to sort this outSqueamish
B
71

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

Bodiless answered 14/9, 2021 at 7:34 Comment(7)
It does not work The Compose file './docker-compose.yaml' is invalid because: Unsupported config option for services.web: 'args'Valuator
@MaximColesnic your args should be under build section, and not under web which your error says. So args is under services.web.build. Watch your indentations.Bodiless
Thank you for this very hard to find information. Exactly what I needed.Adriatic
thanks for this well defined answerCrustal
Please help me to sort this out #72217266Squeamish
Saved the day! Thanks :)Neary
This doesn't seem to work with docker-compose 3.7. ``` jetty: build: jetty args: - CURRENT_PROFILE=$CURRENT_PROFILE - ENV_CONFIG_PATH=$ENV_CONFIG_PATH``` It shows and error - mapping values are not allowed here in 'reader', line 20, column 16: args: ^Hexahydrate
F
69

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.

Fragmentary answered 27/1, 2014 at 18:13 Comment(8)
This doesn't seem to work if you need to ADD files from the directory containing the Dockerfile.Linsang
Very nice solution! On a Mac you can get envsubst as part of brew install gettext. But because of possible conflicts with the BSD build system it is "keg-only" and no symlnks are made. However, it is safe to do ln -s /usr/local/Cellar/gettext/*/bin/envsubst /usr/local/bin/ to add that one command to your PATH. (It's really the libs that are the concern.) Or you can use it in its /usr/local/Cellar/gettext/*/bin/envsubst locationDonatello
To clarify @TomHennen's comment, piping the Dockerfile to docker build - is, specifically, what doesn't work when you reference relative paths from your Dockerfile, regardless of env var substitution.Eisenach
Re @TomHennen's comment, if you do want to use context-dependent commands like COPY in your Dockerfile you could always redirect the output of envsubst to a temporary file and then feed that into docker build instead. Example: cat Dockerfile | envsubst > DockerfileWithEnvVars, then docker build -t my-target -f DockerfileWithEnvVars ., then rm DockerfileWithEnvVarsPasserine
Or you can use sponge from moreutils package envsubst < Dockerfile | sponge DockerfileLammergeier
Adding -f before - seem to fix the issue with the ADD command not working: cat Dockerfile | envsubst | docker build -t my-target . -f -Libelee
If you per the instructions of @Lammergeier use sponge, you'll overwrite the original Dockerfile. Probably not what you want.Elasticity
Please help me to sort this out #72217266Squeamish
B
24

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:

docker build -f <(envsubst < Dockerfile) -t my-target .
Beehive answered 13/3, 2017 at 18:56 Comment(2)
unfortunately that does not seem to work (Docker 17.09), I get the error unable to prepare context: the Dockerfile (/dev/fd/63) must be within the build contextPisano
#72217266 Please help me to sort this outSqueamish
I
17

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
In answered 19/3, 2015 at 22:57 Comment(2)
Remember to give permissions to the shell file. with chmod 775 env.shCuneal
#72217266 Please help me to sort this outSqueamish
C
6

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 -

Callisthenics answered 31/10, 2017 at 19:30 Comment(1)
Best answer here, and based on that, was able to do: envsubst < ./Dockerfile | docker build -squash -t ${DOCKIMG}:${VERSION} . -f - where the FROM line is using the environment variable.Roussillon
M
5

When using build-arg...

docker build --build-arg CODE_VERSION=1.2 Dockerfile

...consider that the variable is not available after FROM:

ARG  CODE_VERSION=latest
FROM base:${CODE_VERSION}

An ARG declared before a FROM is outside of a build stage, so it can’t be used in any instruction after a FROM.

Generally ARGs should be placed after FROM if not required during FROM:

FROM base:xy
ARG  ABC=123

To use the default value of an ARG declared before the first FROM use an ARG instruction without a value inside of a build stage:

ARG VERSION=latest
FROM busybox:$VERSION
ARG VERSION
RUN echo $VERSION > image_version

https://docs.docker.com/engine/reference/builder/#understand-how-arg-and-from-interact

Monachism answered 7/9, 2022 at 11:11 Comment(1)
Also worth noting: "To use an argument in multiple stages, each stage must include the ARG instruction."Langtry
S
2

You can directly pass env variables when container start :

docker run -e [your_variable_name = your_variable_value] [image to instanciate]

Example :

docker run --rm -e TARGET=192.168.1.9 ubuntu env

The output is :

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=36399469e519
TARGET=192.168.1.9
HOME=/root

The target variable was added to your env variables of the current container !

Subjacent answered 10/4, 2023 at 14:7 Comment(0)
W
1

Correct me if I wrong. The question was about env variables - not about args. The question was How to send ENV var to container - it means that container can reference to that variable. So I think the answer should be the following:

  1. there is no need to use ARG cos it is used only during building the image
  2. if you need to pass environment variable to container there is syntax that can be used to run the container: docker run --name [container-name] -e "[variable-name]=[new-value]" [image-name]
Whiffle answered 28/7, 2023 at 10:7 Comment(0)
F
-3

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
Furlana answered 26/1, 2014 at 20:45 Comment(1)
Damien is looking to build an image. -e works with the run command instead.Paulpaula

© 2022 - 2024 — McMap. All rights reserved.