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
117,546 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