docker进阶教程之dockerfile优化镜像大小

本站所有内容来自互联网收集,仅供学习和交流,请勿用于商业用途。如有侵权、不妥之处,请第一时间联系我们删除!Q群:迪思分享

免费资源网 – https://freexyz.cn/
目录一、无优化构建镜像二、不需要输出的指令丢入/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;”]

docker进阶教程之dockerfile优化镜像大小插图

docker进阶教程之dockerfile优化镜像大小插图1

docker进阶教程之dockerfile优化镜像大小插图2

docker进阶教程之dockerfile优化镜像大小插图3

docker进阶教程之dockerfile优化镜像大小插图4

二、不需要输出的指令丢入/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;”]

docker进阶教程之dockerfile优化镜像大小插图5

docker进阶教程之dockerfile优化镜像大小插图6

docker进阶教程之dockerfile优化镜像大小插图7

三、减少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进阶教程之dockerfile优化镜像大小插图8

docker build -t nginx:v3 . 构建镜像

docker images查看镜像

docker进阶教程之dockerfile优化镜像大小插图9

docker进阶教程之dockerfile优化镜像大小插图10

docker进阶教程之dockerfile优化镜像大小插图11

四、多阶段构建

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查看镜像

docker进阶教程之dockerfile优化镜像大小插图12

docker进阶教程之dockerfile优化镜像大小插图13

docker进阶教程之dockerfile优化镜像大小插图14

docker进阶教程之dockerfile优化镜像大小插图15

五、使用更为轻量级的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 查看

总结

免费资源网 – https://freexyz.cn/


© 版权声明
THE END
★喜欢这篇文章吗?喜欢的话,麻烦动动手指支持一下!★
点赞10 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容