【docker】dockerfile优化镜像大小

目录

一、无优化构建镜像

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

 

四、多阶段构建

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值