Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dockerfile ENV variable substitution into another ENV variable

I am learning Docker and facing issue in substituting the value of one env variable into another env variable.

This is my Dockerfile

FROM ubuntu

ENV var_env=Tarun

ENV command="echo Hello $var_env"

CMD ["sh","-c","echo Hello $var_env"]

Now, after building it with tag name "exp", then

sudo docker run -e  "var_env=New Env Value" exp

It gives me correct output as

Hello New Env Value

But, if I see the environment variables associated by executing

sudo docker run -e  "var_env=New Env Value" exp env

It gives me this output:

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=138aa8852d8d
var_env=New Env Value
command=echo Hello Tarun
HOME=/root

In it, the value of var_env environment variable is changed and value of command environment variable remains same.

I need to find a way so that if I change the value of var_env environment variable by the above command, it will update the value of var_env environment variable inside command environment variable also.

For reference: https://docs.docker.com/engine/reference/builder/#environment-replacement

like image 809
Tarun Chawla Avatar asked Oct 21 '19 07:10

Tarun Chawla


People also ask

How do I pass an environment variable in Dockerfile?

Use -e or --env value to set environment variables (default []). If you want to use multiple environments from the command line then before every environment variable use the -e flag. Note: Make sure put the container name after the environment variable, not before that.

Can Dockerfile access 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. Let's see how we can use it to pass value to our greetings script. There are two different ways to do it.

What is ARG in Dockerfile?

You can use the ARG command inside a Dockerfile to define the name of a parameter and its default value. This default value can also be overridden using a simple option with the Docker build command.

What is the difference between ENV and Arg in Dockerfile?

ENV is for future running containers. ARG for building your Docker image. ¶ ENV is mainly meant to provide default values for your future environment variables.


1 Answers

Option 1: at container start

You can use a wrapper script to create your environment variables with the inheritance that you wish. Here is a simple wrapper script

wrapper.sh

#!/bin/bash

# prep your environement variables
export command="echo Hello $var_env"

# run your actual command
echo "Hello $command"

Your dockerfile needs to be adapted to use it

FROM ubuntu
COPY ./wrapper.sh .
ENV var_env=Tarun
ENV command="echo Hello $var_env"
CMD ["sh","-c","./wrapper.sh"]

Option 2: during build

You can archive this by rebuilding your image with different build args. Lets keep your dockerfile almost the same:

FROM ubuntu
ARG var_env=Tarun
ENV command="echo Hello $var_env"
CMD ["sh","-c","echo Hello $var_env"]

and run

docker build -t test .

this gives you your default image as defined in your dockerfile, but your var_env is no longer an environment variable.

next we run

docker build -t test --build-arg var_env="New Env Value" .

this will invalidate the docker cache only from the line in which you have defined your build arg. So keep your definition of your ARG close to where it is used in order to maximize the caching functionality of docker build.

You can find more about build args here: https://docs.docker.com/engine/reference/commandline/build/

like image 94
ckaserer Avatar answered Oct 18 '22 22:10

ckaserer