If conditional in docker file

+2 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 Atul
• 5,530 points
16,069 views

6 answers to this question.

0 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,810 points
0 votes

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
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

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,510 points
76 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
218 views
0 votes
6 answers

docker: executable file not found in $PATH

file not found in $PATH READ MORE

answered May 9 in Docker by anonymous
9,775 views
0 votes
1 answer
0 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
• 56,900 points
188 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 9, 2018 in Docker by DareDev
• 6,810 points
279 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 in Docker by Sirajul
• 30,140 points
88 views