目錄
- 一、無優化構建鏡像
- 二、不需要輸出的指令丟入/dev/null
- 三、減少RUN構建
- 四、多階段構建
- 五、使用更為輕量級的linux發行版本
- 總結
一、無優化構建鏡像
mkdir /mnt/nginx ? vim Dockerfile FROM centos:7 RUN yum install -y gcc pcre pcre-devel devel zlib-devel make ADD nginx-1.22.0.tar.gz /mnt/nginx WORKDIR /mnt/nginx/nginx-1.22.0 #關閉debug日志 RUN sed -i 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc? RUN ./configure --prefix=/usr/local/nginx? RUN make RUN make install EXPOSE 80 VOLUME ["/usr/local/nginx/html"] CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]??
二、不需要輸出的指令丟入/dev/null
FROM centos:7 RUN yum install -y gcc pcre pcre-devel devel zlib-devel make &> /dev/null && yum clean all ADD nginx-1.22.0.tar.gz /mnt/nginx WORKDIR /mnt/nginx/nginx-1.22.0 RUN sed -i 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc RUN ./configure --prefix=/usr/local/nginx &> /dev/null RUN make &> /dev/null RUN make install &> /dev/null RUN rm -rf /mnt/nginx/nginx-1.22.0 EXPOSE 80 VOLUME ["/usr/local/nginx/html"] CMD ["/usr/local/nginx/sbin/nginx""-g","daemon off;"]
三、減少RUN構建
FROM centos:7 ADD nginx-1.22.0.tar.gz /mnt/nginx WORKDIR /mnt/nginx/nginx-1.22.0 RUN yum install -y gcc pcre pcre-devel devel zlib-devel make &> /dev/null && \ ?yum clean all && \ ?sed -i 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && \ ?./configure --prefix=/usr/local/nginx &> /dev/null && \ ?make &> /dev/null && make install &> /dev/null &&\ ?rm -rf /mnt/nginx/nginx-1.22.0 EXPOSE 80 VOLUME ["/usr/local/nginx/html"] CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]
docker build -t nginx:v3 . 構建鏡像
docker images查看鏡像
四、多階段構建
FROM centos:7 as build? ADD nginx-1.22.0.tar.gz /mnt/nginx? WORKDIR /mnt/nginx/nginx-1.22.0 RUN yum install -y gcc pcre pcre-devel devel zlib-devel make &> /dev/null && \ yum clean all &&\ sed -i 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && \ ./configure --prefix=/usr/local/nginx &> /dev/null && \ make &>/dev/null && \ make install &>/dev/null && \ rm -rf /mnt/nginx/nginx-1.22.0 FROM centos:7 EXPOSE 80 VOLUME ["/usr/local/nginx/html"] COPY --from=build /usr/local/nginx /usr/local/nginx CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]
docker build -t nginx:v4?. 構建鏡像 docker images查看鏡像
五、使用更為輕量級的linux發行版本
debian
alpine
apt add
Sandbox
Busybox
#下載debian壓縮包 wget http://download.openvz.org/template/precreated/debian-7.0-x86-minimal.tar.gz docker import debian-7.0-x86-minimal.tar.gz -- debian:v1 生成鏡像 docker images 查看