下面就是我针对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