Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional Block in Docker

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

FROM centos:centos7
MAINTAINER Akshay <[email protected]>

# Update and install required binaries
RUN yum update -y \
    && yum install -y which wget openssh-server sudo java-1.8.0-openjdk \
    && 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 \
        && 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 && \
    wget -q http://archive.apache.org/dist/ant/binaries/apache-ant-${ANT_VERSION}-bin.tar.gz && \
    tar -xzf apache-ant-${ANT_VERSION}-bin.tar.gz && \
    mv apache-ant-${ANT_VERSION} /opt/ant && \
    rm apache-ant-${ANT_VERSION}-bin.tar.gz
ENV ANT_HOME /opt/ant
ENV PATH ${PATH}:/opt/ant/bin
......

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 ARG 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 if/else block.

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 RUN if $BUILDVAR -eq "SO"; then export SOMEVAR=hello; else export SOMEVAR=world; fiLink, or writing a separate script file like this 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

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
like image 950
DMA Avatar asked Mar 10 '17 13:03

DMA


1 Answers

If you don't want to use all those RUN if statements, you can instead create a bash script with the setup procedure and call it from the Dockerfile. For example:

FROM centos:centos7
MAINTAINER Someone <[email protected]>

ARG BUILD_TOOL

COPY setup.sh /setup.sh

RUN ./setup.sh

RUN rm /setup.sh

And the setup.sh file (don't forget to make it executable):

if [ "${BUILD_TOOL}" = "MAVEN" ]; then
    echo "Step 1 of MAVEN setup";
    echo "(...)";
    echo "Done MAVEN setup";
elif [ "${BUILD_TOOL}" = "ANT" ]; then
    echo "Step 1 of ANT setup";
    echo "(...)";
    echo "Done ANT setup";
fi

You can then build it using docker build --build-arg BUILD_TOOL=MAVEN . (or ANT).

Note that I used a shell script here, but if you have other interpreters available (ex: python or ruby), you can also use them to write the setup script.

like image 130
Salem Avatar answered Sep 20 '22 23:09

Salem