docker容器 如何精简镜像减小体积

本文介绍了如何精简Docker镜像以减小其体积,包括减少镜像层数和缩减容量两方面。通过合并指令、使用多阶段构建、选择小型基础镜像、管理上下文以及及时清理下载等方式,有效优化Dockerfile,实现镜像瘦身。
摘要由CSDN通过智能技术生成

 

写在前面

这篇小作文我们会学习镜像构建的另一个关键性问题,为何别人打造的镜像只有10MB而我的有几百MB?如何精简镜像减小镜像体积?


精简镜像我们可以从两个方面切入:

  • 减少镜像层数
  • 缩减容量

一、减少镜像层数

1.指令合并

Dockerfile 中的每条指令都将创建一个层,不过查看官方文档中最佳实践有这样一句话:

In older versions of Docker, it was important that you minimized the number of layers in your images to ensure they were performant. The following features were added to reduce this limitation:

  • Only the instructions RUNCOPYADD create layers. Other instructions create temporary intermediate images, and do not increase the size of the build.

    ...

参考地址:Minimize the number of layers

意味着只有 RUNCOPYADD 三个指令会创建层,其他指令会创建一个中间镜像,并且不会影响镜像大小。这样我们说的指令合并也就是以这三个指令为主。

我们以如下Dockerfile为例

FROM debian:stable

WORKDIR /var/www
LABEL version=“v1”

RUN apt-get update
RUN apt-get -y --no-install-recommends install curl

RUN apt-get purge -y curl
RUN apt-get autoremove -y
RUN apt-g
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值