2

I want to create docker file with conditional statements. i get parameters from outside ( BUILD_TOOL) . This is the code but i got error while building docker image.

i got this error = dockerfile parse error line 23: unknown instruction: ELSE

RUN if [ "$BUILD_TOOL" = "maven" ] ; then 
    RUN mvn clean install;

#if build tool is gradle
else 
    RUN gradle clean;
fi
kavindu
  • 305
  • 2
  • 5
  • 13
  • Dockerfiles don't have conditionals. As @anemyte suggests and the linked question shows, you can `RUN if ...; then ...; else ...; fi` within a single RUN statement, and that's enough for what you show. – David Maze Jan 13 '21 at 14:37

1 Answers1

8

Dokerfile itself does not have conditional statements. However you can implement them in shell:

RUN if [ "$BUILD_TOOL" = "maven" ] ; then \
       echo do something; \
    else \
       echo do something else; \
    fi

Just remember to add \ at the end of each line when you have a multi-line command.

anemyte
  • 17,618
  • 1
  • 24
  • 45