下麵就是我針對docker file同一個文件,按照layer層的個數的多少,分別構建了兩個鏡像的jenkins-master。兩者大小相差300MB。 <1> layer層數太多,沒有將命令合併為一條命令 <2> layer層數很少,將全部命令合併為一條命令 然後,分別執行構建命令 root >> ...
下麵就是我針對docker file同一個文件,按照layer層的個數的多少,分別構建了兩個鏡像的jenkins-master。兩者大小相差300MB。
<1> layer層數太多,沒有將命令合併為一條命令
FROM jenkins/jenkins:2.194 MAINTAINER [email protected] USER root ENV MAVEN_HOME /usr/local/maven ENV JAVA_HOME /usr/local/java ENV M2=/usr/local/maven/repository ENV CLASSPATH .:$JAVA_HOME/lib:$M2_HOME/lib ENV PATH $MAVEN_HOME/bin:$JAVA_HOME/bin:$PATH RUN mkdir -p /usr/local/java RUN mkdir -p /usr/local/maven/repository COPY soft/apache-maven-3.5.4/* /usr/local/ COPY soft/jre1.8.0_212/* /usr/local/java/
<2> layer層數很少,將全部命令合併為一條命令
FROM jenkins/jenkins:2.194 MAINTAINER [email protected] USER root && \ ENV MAVEN_HOME /usr/local/maven && \ ENV JAVA_HOME /usr/local/java && \ ENV M2 /usr/local/maven/repository && \ ENV CLASSPATH .:$JAVA_HOME/lib:$M2_HOME/lib && \ ENV PATH $MAVEN_HOME/bin:$JAVA_HOME/bin:$PATH && \ RUN mkdir -p /usr/local/java && \ RUN mkdir -p /usr/local/maven/repository && \ COPY soft/apache-maven-3.5.4/* /usr/local/ && \ COPY soft/jre1.8.0_212/* /usr/local/java/
然後,分別執行構建命令
root >> cd /opt/dockerfile/jenkins/02/
root >> docker build -f /opt/dockerfile/jenkins/02/jenkins-master-01.Dockerfile . -t registry.cn-hangzhou.aliyuncs.com/kevin_docker_registry/mirror_k8s_jenkins-master:2.194
root >> docker build -f /opt/dockerfile/jenkins/02/jenkins-master-02.Dockerfile . -t registry.cn-hangzhou.aliyuncs.com/kevin_docker_registry/mirror_k8s_jenkins-master:2.194-test
最後,我們檢查結果
發現第二個dockerfile生成的鏡像文件大小要比前者體積小300MB。
root >> docker images
附錄:
如果需要,我們還可以將生成好的jenkins-master提交到阿裡雲的倉庫,命令參考如下
root >> docker login --username=kevin@1522315893340281 registry.cn-hangzhou.aliyuncs.com/kevin_docker_registry/mirror_k8s_jenkins-master root >> docker tag db8450d6f4f0 registry.cn-hangzhou.aliyuncs.com/kevin_docker_registry/mirror_k8s_jenkins-master:2.194 root >> docker push registry.cn-hangzhou.aliyuncs.com/kevin_docker_registry/mirror_k8s_jenkins-master:2.194
上傳完畢後,登錄阿裡雲倉庫,查看鏡像版本,如下
參考資料
https://www.cnblogs.com/ericnie/p/7991218.html