首頁 > 軟體

Dockfile 生成Docker映象檔案大小的比較

2020-06-16 16:31:17

下面就是我針對docker file同一個檔案,按照layer層的個數的多少,分別構建了兩個映象的jenkins-master。兩者大小相差300MB。

<1> layer層數太多,沒有將命令合併為一條命令

FROM jenkins/jenkins:2.194
MAINTAINER tiandong9860806@linuxidc.com
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 tiandong9860806@linuxidc.com
 
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

上傳完畢後,登入阿里雲倉庫,檢視映象版本,如下

 

 

 

 


IT145.com E-mail:sddin#qq.com