我有下面的Dockerfile。
FROM ubuntu:14.04 MAINTAINER Samuel Alexander <samuel@alexander.com> RUN apt-get -y install software-properties-common RUN apt-get -y update # Install Java. RUN echo oracle-java8-installer shared/accepted-oracle-license-v1-1 select true | debconf-set-selections RUN add-apt-repository -y ppa:webupd8team/java RUN apt-get -y update RUN apt-get install -y oracle-java8-installer RUN rm -rf /var/lib/apt/lists/* RUN rm -rf /var/cache/oracle-jdk8-installer # Define working directory. WORKDIR /work # Define commonly used JAVA_HOME variable ENV JAVA_HOME /usr/lib/jvm/java-8-oracle # JAVA PATH ENV PATH /usr/lib/jvm/java-8-oracle/bin:$PATH # Install maven RUN apt-get -y update RUN apt-get -y install maven # Install Open SSH and git RUN apt-get -y install openssh-server RUN apt-get -y install git # clone Spark RUN git clone https://github.com/apache/spark.git WORKDIR /work/spark RUN mvn -DskipTests clean package # clone and build zeppelin fork RUN git clone https://github.com/apache/incubator-zeppelin.git WORKDIR /work/incubator-zeppelin RUN mvn clean package -Pspark-1.6 -Phadoop-2.6 -DskipTests # Install Supervisord RUN apt-get -y install supervisor RUN mkdir -p var/log/supervisor # Configure Supervisord COPY conf/supervisord.conf /etc/supervisor/conf.d/supervisord.conf # bash RUN sed -i s#/home/git:/bin/false#/home/git:/bin/bash# /etc/passwd EXPOSE 8080 8082 CMD ["/usr/bin/supervisord"]
构建图像时,它在步骤23中失败,即
RUN mvn clean package -Pspark-1.6 -Phadoop-2.6 -DskipTests
现在,当我重建时,由于docker使用缓存,它从步骤23开始构建。
但是,如果我想从步骤21重建图像,即
RUN git clone https://github.com/apache/incubator-zeppelin.git
我怎样才能做到这一点?删除缓存的图像是唯一的选择吗?是否有其他参数可以做到这一点?
您可以通过执行以下操作来重建整个内容,而无需使用缓存
docker build --no-cache -t user/image-name
要强制从特定行开始重新运行,可以传递一个未使用的arg。Docker将ARG值作为环境变量传递给RUN命令,因此更改ARG就是对命令的更改,这会中断缓存。甚至不必在RUN行上自己定义它。
FROM ubuntu:14.04 MAINTAINER Samuel Alexander <samuel@alexander.com> RUN apt-get -y install software-properties-common RUN apt-get -y update # Install Java. RUN echo oracle-java8-installer shared/accepted-oracle-license-v1-1 select true | debconf-set-selections RUN add-apt-repository -y ppa:webupd8team/java RUN apt-get -y update RUN apt-get install -y oracle-java8-installer RUN rm -rf /var/lib/apt/lists/* RUN rm -rf /var/cache/oracle-jdk8-installer # Define working directory. WORKDIR /work # Define commonly used JAVA_HOME variable ENV JAVA_HOME /usr/lib/jvm/java-8-oracle # JAVA PATH ENV PATH /usr/lib/jvm/java-8-oracle/bin:$PATH # Install maven RUN apt-get -y update RUN apt-get -y install maven # Install Open SSH and git RUN apt-get -y install openssh-server RUN apt-get -y install git # clone Spark RUN git clone https://github.com/apache/spark.git WORKDIR /work/spark RUN mvn -DskipTests clean package # clone and build zeppelin fork, changing INCUBATOR_VER will break the cache here ARG INCUBATOR_VER=unknown RUN git clone https://github.com/apache/incubator-zeppelin.git WORKDIR /work/incubator-zeppelin RUN mvn clean package -Pspark-1.6 -Phadoop-2.6 -DskipTests # Install Supervisord RUN apt-get -y install supervisor RUN mkdir -p var/log/supervisor # Configure Supervisord COPY conf/supervisord.conf /etc/supervisor/conf.d/supervisord.conf # bash RUN sed -i s#/home/git:/bin/false#/home/git:/bin/bash# /etc/passwd EXPOSE 8080 8082 CMD ["/usr/bin/supervisord"]
然后使用唯一的arg运行它:
docker build --build-arg INCUBATOR_VER=20160613.2 -t user/image-name .
要在每次构建时更改参数,您可以将时间戳作为arg传递:
docker build --build-arg INCUBATOR_VER=$(date +%Y%m%d-%H%M%S) -t user/image-name .
要么:
docker build --build-arg INCUBATOR_VER=$(date +%s) -t user/image-name .
顺便说一句,我建议进行以下更改以使您的图层更小,RUN在下载并安装后,您可以在单个命令上合并清理和删除步骤的内容越多,最终映像就越小。否则,您的图层将包括下载和清理之间的所有中间步骤:
RUN
FROM ubuntu:14.04 MAINTAINER Samuel Alexander <samuel@alexander.com> RUN DEBIAN_FRONTEND=noninteractive \ apt-get -y install software-properties-common && \ apt-get -y update # Install Java. RUN echo oracle-java8-installer shared/accepted-oracle-license-v1-1 select true | debconf-set-selections && \ add-apt-repository -y ppa:webupd8team/java && \ apt-get -y update && \ DEBIAN_FRONTEND=noninteractive \ apt-get install -y oracle-java8-installer && \ apt-get clean && \ rm -rf /var/lib/apt/lists/* && \ rm -rf /var/cache/oracle-jdk8-installer && \ # Define working directory. WORKDIR /work # Define commonly used JAVA_HOME variable ENV JAVA_HOME /usr/lib/jvm/java-8-oracle # JAVA PATH ENV PATH /usr/lib/jvm/java-8-oracle/bin:$PATH # Install maven RUN apt-get -y update && \ DEBIAN_FRONTEND=noninteractive \ apt-get -y install maven \ openssh-server \ git \ supervisor && \ apt-get clean && \ rm -rf /var/lib/apt/lists/* # clone Spark RUN git clone https://github.com/apache/spark.git WORKDIR /work/spark RUN mvn -DskipTests clean package # clone and build zeppelin fork ARG INCUBATOR_VER=unknown RUN git clone https://github.com/apache/incubator-zeppelin.git WORKDIR /work/incubator-zeppelin RUN mvn clean package -Pspark-1.6 -Phadoop-2.6 -DskipTests # Configure Supervisord RUN mkdir -p var/log/supervisor COPY conf/supervisord.conf /etc/supervisor/conf.d/supervisord.conf # bash RUN sed -i s#/home/git:/bin/false#/home/git:/bin/bash# /etc/passwd EXPOSE 8080 8082 CMD ["/usr/bin/supervisord"]