Conditional logic in Dockerfile, using --build-arg

Alexander Mills

Say I have this:

ARG my_user="root"  # my_user => default is "root"
USER $my_user
ENV USER=$my_user

All good so far, but now we get here:

ENV HOME="/root"

is there a way to do something like this:

ENV HOME $my_user === "root"? "/root" : "/home/$my_user"

Obviously, that's the wrong syntax.

The only solution I can think of is to just use two --build-args, something like this:

docker build -t zoom \
    --build-arg my_user="foo"  \
    --build-arg my_home="/home/foo"  \
     .
Tarun Lalwani

Unfortunately you can't do this directly

https://forums.docker.com/t/how-do-i-send-runs-output-to-env-in-dockerfile/16106/3

So you have two alternatives

Use a shell script at start

You can use a shell script at the start

CMD /start.sh

And in your start.sh you can have that logic

if [ $X == "Y" ]; then
   export X=Y
else
   export X=Z
fi

Create a profile environment variable

FROM alpine

RUN echo "export NAME=TARUN" > /etc/profile.d/myenv.sh
SHELL ["/bin/sh", "-lc"]
CMD env

And then you when you run it

$ docker run test
HOSTNAME=d98d44fa1dc9
SHLVL=1
HOME=/root
PAGER=less
PS1=\h:\w\$
NAME=TARUN
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PWD=/
CHARSET=UTF-8

Note: The SHELL ["/bin/sh", "-lc"] is quite important here, else the profile will not be loaded

Note2: Instead of RUN echo "export NAME=TARUN" > /etc/profile.d/myenv.sh you can also do a COPY myevn.sh /etc/profile.d/myenv.sh and have the file be present in your build context

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Conditional ARG in Dockerfile?

Conditional npm install ARG in Dockerfile

Using ARG and ENV in Dockerfile

Select ENV/ARG in Dockerfile depending on build ARG

Dockerfile build ARG in COPY --from=

Using ARG command in a sub Dockerfile

Using ARG in my Dockerfile but still getting "Failed to replace env in config" when running my build

DockerFile: Passing an ARG to dotnet build for build-configuration

Overwriting of environment variables in Dockerfile with passed by `--build-arg`

Dockerfile: How to replace a placeholder in environment variable with build-arg's?

Use --build-arg value in Dockerfile FROM parameter

Using COPY on dockerfile for apache build

envsubst within docker using build-arg

is it ok to use conditional logic inside a using statement

Using conditional logic in computed properties fails to update

Altering a value in R using conditional logic

Using conditional if/else logic with pandas dataframe columns

Fill new column in dataframe using conditional logic

Use an argument from docker-compose build --build-arg inside a Dockerfile

Dockerfile not passing ARG to CMD

Dockerfile - ARG SHA and Curl

Dockerfile Overwrite ARG values

Passing an ARG into CMD for the Dockerfile

Dockerfile, ARG and ENV not working

What are ARG and LABEL in a Dockerfile

Why can't I use the build arg again after FROM in a Dockerfile?

Dockerfile --build-arg argument passed for WORKDIR during image creation doesn't work

error using gcp cloud build to create dockerfile

How build a Dockerfile in a Subdirectory using a Jenkinsfile