docker进阶教程之dockerfile优化镜像大小
一、无优化构建镜像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;"] https://img.jbzj.com/file_images/article/202210/2022102710334318.png
https://img.jbzj.com/file_images/article/202210/2022102710334319.png
https://img.jbzj.com/file_images/article/202210/2022102710334320.png
https://img.jbzj.com/file_images/article/202210/2022102710334321.png
https://img.jbzj.com/file_images/article/202210/2022102710334322.png
二、不需要输出的指令丢入/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;"]https://img.jbzj.com/file_images/article/202210/2022102710334323.png
https://img.jbzj.com/file_images/article/202210/2022102710334324.png
https://img.jbzj.com/file_images/article/202210/2022102710334325.png
三、减少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;"]https://img.jbzj.com/file_images/article/202210/2022102710334326.png
docker build -t nginx:v3 . 构建镜像
docker images查看镜像
https://img.jbzj.com/file_images/article/202210/2022102710334327.png
https://img.jbzj.com/file_images/article/202210/2022102710334328.png
https://img.jbzj.com/file_images/article/202210/2022102710334329.png
四、多阶段构建
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查看镜像https://img.jbzj.com/file_images/article/202210/2022102710334330.png
https://img.jbzj.com/file_images/article/202210/2022102710334431.png
https://img.jbzj.com/file_images/article/202210/2022102710334432.png
https://img.jbzj.com/file_images/article/202210/2022102710334433.png
五、使用更为轻量级的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 查看
总结
到此这篇关于docker进阶教程之dockerfile优化镜像大小的文章就介绍到这了,更多相关dockerfile优化镜像大小内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
来源:https://www.jb51.net/article/266061.htm
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页:
[1]