If conditional in docker file

+4 votes
RUN if [ "$AUTH_MS_PROFILE" = "test" ]; then RUN ["mvn", "verify"]; fi

I'm trying to to have two images for production and testing environments. Now, because I don't need integration test for production env, I'm using build-arg to set dev and test profiles, therefore I require if conditional if the input is test it should test otherwise it should not.

May 29, 2018 in Docker by ffdfd
• 5,550 points
119,825 views

7 answers to this question.

+2 votes

First of all, create a build_internal.sh file and paste all such conditionals to it

if [ "$AUTH_MS_PROFILE" = "test" ]; then 
   mvn verify
fi

Copy this file inside and then run it inside the dockerfile. I you want to try what you were trying then do:

RUN if [ "$AUTH_MS_PROFILE" = "test" ]; then mvn verify ; fi
answered May 29, 2018 by DareDev
• 6,890 points
+1 vote

Try this:

FROM centos:7
ARG arg
RUN if [ "x$arg" = "x" ] ; then echo Argument not provided ; else echo Argument is $arg ; fi

and then build the image as:

docker build -t my_docker .
answered Dec 10, 2018 by Naresh
0 votes

You can build it this way:

docker build --build-arg HTTP_PROXY=http://10.20.30.2:1234 .
answered Dec 10, 2018 by Hrithik
0 votes

Create a bash script:

#!/bin/bash -x

if test -z $1 ; then 
    echo "argument empty"
    ........
else 
    echo "Arg not empty: $1"
    ........
fi
Dockerfile:
FROM ...
....
ARG arg
COPY bash.sh /tmp/  
RUN chmod u+x /tmp/bash.sh && /tmp/bash.sh $arg
answered Dec 10, 2018 by Kushal
0 votes

you can do this inside the Dockerfile

ARG argname=false   #default argument when not provided in the --build-arg
RUN if [ "$argname" = "false" ] ; then echo 'false'; else echo 'true'; fi

and in the docker build:

docker build --pull -f "Dockerfile" --label "service_name=${SERVICE_NAME}" -t $SERVICE_NAME --build-arg argname=true .
answered Dec 10, 2018 by Archana
RUN if [ "$argname" = "false" ] ; then echo 'false'; else echo 'true'; fi

Can we split this into mutiple line like :

RUN if [ "$argname" = "false" ] ; 
then 
RUN something
RUN something2
;
else 

RUN something3

RUN something4

; fi
0 votes

You can use the test command

RUN test -z "$VAR" || echo "arg is set" && echo "var is not set"
RUN test -z "$VAR" && echo "arg is not set" || :
RUN test -z "$VAR" || echo "arg is set" && :
answered Dec 10, 2018 by Shushant
0 votes

Hi,

Sometimes when running a Dockerfile you need to run some conditional logic based upon an ARG variable. This can be handy in the instance you want to run a production build or development build or need to run an extra step for production, etc.

ARG ENV
RUN if [ "$ENV" = "production" ] ; then yarn client:build:prod ; else yarn client:build ; fi
answered Dec 14, 2020 by MD
• 95,440 points

Related Questions In Docker

0 votes
1 answer

Different file owner inside Docker container and in host machine Ask

Filesystems, at least in Unix- and Linux-like ...READ MORE

answered Jun 25, 2018 in Docker by Damon Salvatore
• 5,980 points
717 views
0 votes
1 answer

Different file owner inside Docker container and in host machine

Here is what you can try. Since ...READ MORE

answered Jun 27, 2018 in Docker by Atul
• 10,240 points
1,717 views
0 votes
7 answers

docker: executable file not found in $PATH

try to build the image with --no-cache. I ...READ MORE

answered Nov 1, 2020 in Docker by Deeptesh Agrawal
61,060 views
+2 votes
1 answer
+2 votes
1 answer

Deploy Docker Containers from Docker Cloud

To solve this problem, I followed advice ...READ MORE

answered Sep 3, 2018 in AWS by Priyaj
• 58,090 points
2,435 views
0 votes
1 answer

No output in output file even though pexpect scrift runs succesfully

You've got to wait til the ls ...READ MORE

answered Aug 10, 2018 in Docker by DareDev
• 6,890 points
3,378 views
0 votes
2 answers

How to open docker containers file system?

Adding on to @DareDev's answer there's one ...READ MORE

answered Aug 5, 2019 in Docker by Sirajul
• 59,230 points
20,072 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP