是否可以减小 Docker 映像的大小?

Edd*_*onk 1 docker

我正在尝试创建一个精简的 Centos 7.4 图像。我想删除我不需要的东西,并想保留 Python 和 Yum。首先,我想摆脱 systemd。

我正在建立我的形象:

sudo docker build --squash --compress -t smaller-centos smaller-centos
Run Code Online (Sandbox Code Playgroud)

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"]
Run Code Online (Sandbox Code Playgroud)

我删除了一些图像,但新图像仍然比原始图像大。

$ 须藤泊坞窗图像

smaller-centos      latest              59c3425f7909        2 minutes ago       219MB
centos              latest              e934aafc2206        4 weeks ago         199MB
Run Code Online (Sandbox Code Playgroud)

$ sudo docker 版本

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
Run Code Online (Sandbox Code Playgroud)

小智 5

如果您至少使用17.05版本,多阶段功能可以为您提供一个简单的解决方案......

只需将其添加到 Dockerfile 的末尾:

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

CMD ["/bin/bash"]
Run Code Online (Sandbox Code Playgroud)

这将做“伎俩”。不需要“--squash”选项。只需执行以下命令:

sudo docker build -t smaller-centos smaller-centos
Run Code Online (Sandbox Code Playgroud)

最终结果是预期的 178MB 图像。