大网数据广告活动
服务器运维问答

Is it possible to reduce a Docker image in size?

· 52CE 我爱测

I'm trying to create a stripped down Centos 7.4 image. I want to remove the stuff I don't need and want to keep Python and Yum. To start I wanted to get rid of systemd. I'm building my image with: sudo docker build --squash --com…

问题原文

原帖链接 · Eddy Pronk · CC BY-SA 4.0

I'm trying to create a stripped down Centos 7.4 image. I want to remove the stuff I don't need and want to keep Python and Yum. To start I wanted to get rid of systemd.

I'm building my image with:

sudo docker build --squash --compress -t smaller-centos smaller-centos

Dockerfile:

FROM centos

RUN rpm -e --nodeps systemd
RUN yum remove -y dracut-033-502.el7_4.1.x86_64 iputils-20160308-10.el7.x86_64 device-mapper-1.02.140-8.el7.x86_64  dbus-1.6.12-17.el7.x86_64 kpartx-0.4.9-111.el7_4.2.x86_64 cryptsetup-libs-1.7.4-3.el7_4.1.x86_64 kpartx-0.4.9-111.el7_4.2.x86_64 acl-2.2.51-12.el7.x86_64 bind-license-9.9.4-51.el7_4.2.noarch cryptsetup-libs-1.7.4-3.el7_4.1.x86_64dbus-1.6.12-17.el7.x86_64 dbus-python-1.1.1-9.el7.x86_64 hostname-3.13-3.el7.x86_64 iputils-20160308-10.el7.x86_64 kmod-libs-20-15.el7_4.7.x86_64 passwd-0.79-4.el7.x86_64 python-gobject-base-3.22.0-1.el7_4.1.x86_64 qrencode-libs-3.4.1-3.el7.x86_64 rootfiles-8.1-11.el7.noarch vim-minimal-7.4.160-2.el7.x86_64
RUN yum clean all

CMD ["/bin/bash"]

I removed some images, but the new image is still larger than the original one.

$ sudo docker images

smaller-centos      latest              59c3425f7909        2 minutes ago       219MB
centos              latest              e934aafc2206        4 weeks ago         199MB

$ sudo docker version

Client:
 Version:      18.03.1-ce
 API version:  1.37
 Go version:   go1.9.5
 Git commit:   9ee9f40
 Built:        Thu Apr 26 07:17:38 2018
 OS/Arch:      linux/amd64
 Experimental: true
 Orchestrator: swarm

Server:
 Engine:
  Version:      18.03.1-ce
  API version:  1.37 (minimum version 1.12)
  Go version:   go1.9.5
  Git commit:   9ee9f40
  Built:        Thu Apr 26 07:15:45 2018
  OS/Arch:      linux/amd64
  Experimental: true

已采纳解答

原帖链接 · João Alves · CC BY-SA 4.0

If you are using at least version 17.05, the multi-stage feature can give you a simple solution...

Just add this to the end of Dockerfile:

[...]
FROM scratch
COPY --from=0 / /

CMD ["/bin/bash"]

This will do the "trick". No need for "--squash" option. Just execute the command:

sudo docker build -t smaller-centos smaller-centos

The final result is the expected 178MB image.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-05-05)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。