docker-17-docker镜像优化减小镜像体积

10 个优化技巧,减少 Docker 镜像大小
Docker镜像优化:从1.16GB到22.4MB!
参考Docker容器镜像的本质是什么?
MySQL运行在docker容器中会损失多少性能
docker python alpine numpy报错解决
Docker操作系统之Alpine

1 Docker容器镜像的本质

Docker镜像是一个只读的文件系统,是由一层一层的文件系统组成,每一层仅包含了前一层的差异部分,这种层级的文件系统被称为UnionFS。大多数Docker镜像都是在base镜像的基础进行构建,每进行一次新的创建就会在镜像上构建一个新的UnionFS。
通过命令查看镜像的构建过程,这里使用history与镜像的ID号组合的命令查看镜像构建过程,所显示的信息包括镜像ID、创建时间、由什么命令创建以及镜像大小。
在这里插入图片描述

2 减少Docker镜像大小

2.1 最小化镜像层

我们可以减少 Dockerfile 中的层数。
dockerfile 中的每个 FROM、RUN、COPY 命令都会创建一个单独的层,并增加镜像的整体大小和构建时间。

要减小docker镜像大小,请在单个RUN或COPY指令中执行多个命令来最小化Dockerfile中的层数。

2.1.1 dockerfile1分散

FROM python:3.9-slim
RUN pip3 install pip -U
RUN pip3 config set global.index-url http://mirrors.aliyun.com/pypi/simple
RUN pip3 config set install.trusted-host mirrors.aliyun.com
RUN pip3 install flask
RUN pip3 install scikit-learn==1.1.2
RUN pip3 install pandas==1.4.4
RUN pip3 install numpy==1.23.2
RUN pip3 install scipy==1.9.1
RUN pip3 install redis==4.3.4
RUN pip3 install pymysql==1.0.2

docker build -f dockerfile1 -t python3.9:old .
构建过程如下:
在这里插入图片描述

2.1.2 dockerfile2合并

FROM python:3.9-slim
RUN pip3 install pip -U
RUN pip3 config set global.index-url http://mirrors.aliyun.com/pypi/simple
RUN pip3 config set install.trusted-host mirrors.aliyun.com
RUN pip3 install flask
RUN pip3 install scikit-learn==1.1.2 && \
pip3 install pandas==1.4.4 && \
pip3 install numpy==1.23.2 && \
pip3 install scipy==1.9.1 && \
pip3 install redis==4.3.4 && \
pip3 install pymysql==1.0.2

docker build -f dockerfile2 -t python3.9:new .

在这里插入图片描述
通过最小化镜像层可以显著降低镜像大小。
在这里插入图片描述

2.2 禁用缓存

2.2.1 dockerfile3分散

在–no-cache-dir构建Docker映像时有充分的理由使用。缓存在Docker映像中通常是无用的,并且您可以通过禁用缓存来缩小映像大小。

FROM python:3.9-slim
RUN pip3 install pip -U
RUN pip3 config set global.index-url http://mirrors.aliyun.com/pypi/simple
RUN pip3 config set install.trusted-host mirrors.aliyun.com
RUN pip3 install --no-cache-dir flask
RUN pip3 install --no-cache-dir scikit-learn==1.1.2
RUN pip3 install --no-cache-dir pandas==1.4.4
RUN pip3 install --no-cache-dir numpy==1.23.2
RUN pip3 install --no-cache-dir scipy==1.9.1
RUN pip3 install --no-cache-dir redis==4.3.4
RUN pip3 install --no-cache-dir pymysql==1.0.2

docker build -f dockerfile3 -t python3.9:old1 .
在这里插入图片描述

2.2.2 dockerfile4合并

FROM python:3.9-slim
RUN pip3 install pip -U
RUN pip3 config set global.index-url http://mirrors.aliyun.com/pypi/simple
RUN pip3 config set install.trusted-host mirrors.aliyun.com
RUN pip3 install --no-cache-dir flask
RUN pip3 install --no-cache-dir scikit-learn==1.1.2 && \
pip3 install --no-cache-dir pandas==1.4.4 && \
pip3 install --no-cache-dir numpy==1.23.2 && \
pip3 install --no-cache-dir scipy==1.9.1 && \
pip3 install --no-cache-dir redis==4.3.4 && \
pip3 install --no-cache-dir pymysql==1.0.2

docker build -f dockerfile4 -t python3.9:new1 .
在这里插入图片描述

2.3 使用Docker Squash减小镜像大小

Docker在构建镜像时创建了很多层。
压缩有助于在逻辑层中组织镜像。
我们可以控制镜像的结构,而不是让镜像具有多个不必要的层。
做软链接的目的是为了在终端直接执行。

安装pip install docker-squash。
链接ln -s /home/zb/.local/bin/docker-squash /usr/bin/docker-squash

docker-squash python3.9:old -t python3.9:old-press
docker-squash python3.9:old1 -t python3.9:old1-press
docker-squash python3.9:new -t python3.9:new-press
docker-squash python3.9:new1 -t python3.9:new1-press

在这里插入图片描述

2.4 使用较小的基础镜像

减小docker镜像大小最明显的方法是使用较小的基础镜像。
如果希望为 python 应用程序创建镜像,请考虑使用 python:3.9-slim 镜像而不是 python:3.9。
您可以使用 alpine 版本进一步减少镜像。alpine 镜像是专门为作为容器运行而设计的,而且体积非常小。
python:3.9 的大小约为 1.3 GB,而 python:3.9-slim 的大小仅为 1 GB 左右。
python:3.9-alpine 镜像只有 49 MB。
在这里插入图片描述
进入容器内部:

 docker run -id --name=f1 python:3.9-alpine /bin/sh
 docker exec -it f1 /bin/sh

alpine提供了非常好用的apk软件包管理工具,可以方便地安装、删除、更新软件。

FROM python:3.9-alpine
RUN pip3 install pip -U
RUN pip3 config set global.index-url http://mirrors.aliyun.com/pypi/simple
RUN pip3 config set install.trusted-host mirrors.aliyun.com
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
RUN apk add --no-cache g++ 
RUN pip3 install --no-cache-dir numpy==1.23.2

在这里插入图片描述
pypi中没有对应版本的预编译whl文件,所以会自动构建,构建过程如下:
在这里插入图片描述
如果不指定版本,就会寻找合适的whl文件,不再自动构建。
在这里插入图片描述

在这里插入图片描述

FROM python:3.9-alpine
RUN pip3 install pip -U
RUN pip3 config set global.index-url http://mirrors.aliyun.com/pypi/simple
RUN pip3 config set install.trusted-host mirrors.aliyun.com
RUN apk add --no-cache g++ && \
pip3 install --no-cache-dir flask && \
pip3 install --no-cache-dir scikit-learn==1.1.2 && \
pip3 install --no-cache-dir pandas==1.4.4 && \
pip3 install --no-cache-dir numpy==1.23.2 && \
pip3 install --no-cache-dir scipy==1.9.1 && \
pip3 install --no-cache-dir redis==4.3.4 && \
pip3 install --no-cache-dir pymysql==1.0.2
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

皮皮冰燃

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值