如何缩小 RHEL/Centos/Fedora Docker 图片

How to reduce the size of RHEL/Centos/Fedora Docker image

Red Hat 的基本映像非常小,RHEL 7.4 大约为 196M。然而,它往往会遗漏很多我要为其构建新图像的产品所需的细节。

当我在它上面做一个 "yum install Xxx" 时,图像大小增加了 +500M-800M。

有没有办法缩小图片的大小?

是 Docker 可以通过 "yum clean all"

显着减小图像尺寸

初始 RHEL 映像大小 = 196M

Docker文件 - RHEL 镜像(+bc) = 505M

# Build command
# docker build -t rhel7base:latest --build-arg REG_USER='<redhat_developer_user>' --build-arg REG_PSWD='<password>' --squash .

FROM registry.access.redhat.com/rhel7/rhel:latest

LABEL maintainer="tim"

ARG REG_USER=none
ARG REG_PSWD=none

RUN subscription-manager register --username $REG_USER --password $REG_PSWD --auto-attach && \
    subscription-manager repos --enable rhel-server-rhscl-7-rpms && \
    yum install -y bc

Docker文件 - RHEL Image(+bc) with "yum clean all" = 207M saving 298M

# Build command
# docker build -t rhel7base:latest --build-arg REG_USER='<redhat_developer_user>' --build-arg REG_PSWD='<password>' --squash .

FROM registry.access.redhat.com/rhel7/rhel:latest

LABEL maintainer="tim"

ARG REG_USER=none
ARG REG_PSWD=none

RUN subscription-manager register --username $REG_USER --password $REG_PSWD --auto-attach && \
    subscription-manager repos --enable rhel-server-rhscl-7-rpms && \
    yum install -y bc && \
    yum clean all && \
    rm -rf /var/cache/yum

注意:--squash 选项在 Docker 的最新版本中作为实验标志出现。它将分层文件系统压缩成一个新层 https://blog.docker.com/2017/01/whats-new-in-docker-1-13/

我在 https://medium.com/@vaceletm/docker-layers-cost-b28cb13cb627

找到了使用 "yum clean all" 的解决方案

"rm -rf /var/cache/yum" 的添加来自 "yum clean all"

输出中的建议

除了TJA的回答,你也可以使用一个更小的CentOS基础镜像,例如,有一个Debian light,叫做Bitnami

https://hub.docker.com/r/bitnami/minideb-extras/

对于 CentOS 也许你可以使用

https://hub.docker.com/r/blalor/centos/

您也可以尝试使用 2 种工具缩小图片大小

https://github.com/mvanholsteijn/strip-docker-image

https://github.com/docker-slim/docker-slim