title: Conditional Block in Docker
body: <p>I have been using docker since some time now. I have encountered a situation wherein I need to execute instructions present in the Dockerfile based on some condition. For example here is the snippet of Dockerfile</p>
  
  <pre><code>FROM centos:centos7
  MAINTAINER Akshay &lt;akshay@dm.com&gt;
  
  # Update and install required binaries
  RUN yum update -y \
      &amp;&amp; yum install -y which wget openssh-server sudo java-1.8.0-openjdk \
      &amp;&amp; yum clean all
  
  #install Maven
  RUN curl -Lf http://archive.apache.org/dist/maven/maven-3/3.3.9/binaries/apache-maven-3.3.9-bin.tar.gz -o /tmp/apache-maven-3.3.9.tar.gz
  RUN tar -xzf /tmp/apache-maven-3.3.9.tar.gz -C /opt \
          &amp;&amp; rm /tmp/apache-maven-3.3.9.tar.gz
  
  ENV M2_HOME "/opt/apache-maven-3.3.9"
  ENV PATH ${PATH}:${M2_HOME}/bin:
  
  # Install Ant
  ENV ANT_VERSION 1.9.4
  RUN cd &amp;&amp; \
      wget -q http://archive.apache.org/dist/ant/binaries/apache-ant-${ANT_VERSION}-bin.tar.gz &amp;&amp; \
      tar -xzf apache-ant-${ANT_VERSION}-bin.tar.gz &amp;&amp; \
      mv apache-ant-${ANT_VERSION} /opt/ant &amp;&amp; \
      rm apache-ant-${ANT_VERSION}-bin.tar.gz
  ENV ANT_HOME /opt/ant
  ENV PATH ${PATH}:/opt/ant/bin
  ......
  </code></pre>
  
  <p>So as you can see in my docker file that I have installation instructions for both maven and ant. But now I've to install either one of them based on a condition. I know that I can use <code>ARG</code> instruction in the Dockerfile to fetch the argument during the build time, but the problem is I couldn't find any docs on how to enclose them in an <code>if/else</code> block.</p>
  
  <p>I have read few other stackoverflow posts too regarding this, but in those I see that they have asked to use conditional statements inside of an instruction eg <code>RUN if $BUILDVAR -eq "SO"; then export SOMEVAR=hello; else export SOMEVAR=world; fi</code><a href="https://stackoverflow.com/a/30157278/2834020">Link</a>, or writing a separate script file like <a href="https://stackoverflow.com/a/41256314">this</a>
  But as you can see, my case is different. I can't make us of this since I would have a bunch of other instructions too which would be depended on that argument. I have to do something like this </p>
  
  <pre><code>ARG BUILD_TOOL  
  if [ "${BUILD_TOOL}" = "MAVEN" ]; then
  --install maven
  elif [ "${BUILD_TOOL}" = "ANT" ]; then
  --install ant
  fi
  .... other instructions ....
  if [ "${BUILD_TOOL}" = "MAVEN" ]; then
      --some other dependent commands
  elif [ "${BUILD_TOOL}" = "ANT" ]; then
      --some other dependent commands
  fi
  </code></pre>