同样功能的镜像,但是不同的Dockerfilebuild出来的镜像大小是不一样的,下面这篇文章主要给大家介绍了关于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;"]
二、不需要输出的指令丢入/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 查看
总结
到此这篇关于docker进阶教程之dockerfile优化镜像大小的文章就介绍到这了,更多相关dockerfile优化镜像大小内容请搜索编程学习网以前的文章希望大家以后多多支持编程学习网!
沃梦达教程
本文标题为:docker进阶教程之dockerfile优化镜像大小
基础教程推荐
猜你喜欢
- centos 7 安装及配置zabbix agent 2023-09-24
- 实战Nginx_取代Apache的高性能Web服务器 2023-09-29
- apache和nginx结合使用 2023-09-10
- linux下安装apache与php;Apache+PHP+MySQL配置攻略 2023-08-07
- IIS 6 的 PHP 最佳配置方法 2022-09-01
- 服务器添加git钩子的步骤 2022-12-12
- Apache Hudi数据布局黑科技降低一半查询时间 2022-10-06
- linux之conda环境安装全过程 2023-07-11
- nginx.conf(centos7 1.14)主配置文件修改 2023-09-23
- Apache服务器配置攻略3 2022-09-01