Docker中的条件块 [英] Conditional Block in Docker
问题描述
从现在开始我一直在使用docker。我遇到了一种情况,我需要根据某种条件执行Dockerfile中存在的指令。例如,这是Dockerfile的代码段
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 <akshay@dm.com>
# 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
......
因此您可以在我的docker文件中看到我已经安装了有关Maven和ant的说明。但是现在我必须根据条件安装其中任何一个。我知道我可以在构建文件期间使用Dockerfile中的 ARG
指令来获取参数,但问题是我找不到任何有关如何将其封装在其中的文档 if / else
块。
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.
我也读过一些其他关于这个的stackoverflow帖子,但是在那些我看到的文章中如果$ BUILDVAR -eq SO,他们要求在指令内使用条件语句,例如 RUN;然后导出SOMEVAR = hello;否则,导出SOMEVAR = world; fi
链接,或编写单独的脚本文件,例如此
但是,正如您所看到的,我的情况有所不同。我之所以无法做到这一点,是因为我还有很多其他说明,具体取决于该论点。我必须做这样的事情
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; fi
Link, 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
推荐答案
如果您不想使用所有 RUN if
语句,则可以使用设置过程创建一个bash脚本,并从Dockerfile中调用它。例如:
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 <someone@email.com>
ARG BUILD_TOOL
COPY setup.sh /setup.sh
RUN ./setup.sh
RUN rm /setup.sh
和 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
然后可以使用 docker build进行构建- build-arg BUILD_TOOL = MAVEN。
(或 ANT
)。
请注意,我在这里使用了shell脚本,但是如果您还有其他解释器(例如python或ruby),也可以使用它们来编写安装脚本。
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.
这篇关于Docker中的条件块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!