減少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