减少Docker镜像层的数量

减少Docker镜像层的数量
Dokcerfile中的RUN、COPY和ADD命令才会创建镜像层,因此减少Docker镜像层的数量就是要减少这几个命令的次数,特别是RUN命令的次数。

在安装工具时可以在一句命令中安装多个工具:

正例:

apt-get install -y git curl

反例:

apt-get install -y git
apt-get install -y curl

用&&拼接多个命令:

正例:
RUN apt-get update && apt-get upgrade -y && apt-get install -y git curl

反例:
RUN apt-get update
RUN apt-get upgrade -y
RUN apt-get install -y git curl

用来在拼接多个命令时换行,增加可读性

RUN curl -fsSL https://github.com/krallin/tini/releases/download/${TINI_VERSION}/tini-static-$(dpkg --print-architecture) -o /sbin/tini
&& curl -fsSL https://github.com/krallin/tini/releases/download/${TINI_VERSION}/tini-static-$(dpkg --print-architecture).asc -o /sbin/tini.asc
&& gpg --no-tty --import ${JENKINS_HOME}/tini_pub.gpg
&& gpg --verify /sbin/tini.asc
&& rm -rf /sbin/tini.asc /root/.gnupg
&& chmod +x /sbin/tini

-----------------------------------------------------------------------------
交流QQ号:1175372067
电话:18551717618(同微信)
元几科技.软件3部
迈开步伐去探索
南京元几科技有限公司
元几社区: https://www.cnblogs.com/colin-vio
元几官网: http://www.yuanji.tech
-----------------------------------------------------------------------------
原文地址:https://www.cnblogs.com/colin-vio/p/14421231.html