docker - 如何减小 RHEL/Centos/Fedora Docker 镜像的大小

标签 docker centos redhat fedora

来自 Red Hat 的基本镜像非常小,对于 RHEL 7.4 大约为 196M。但是,它往往会丢失我想要为其构建新图像的产品所需的许多零碎。

当我在它上面执行“yum install Xxx”时,图像大小会爆炸到 +500M-800M。

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

最佳答案

是的,通过执行“yum clean all”可以显着减小 Docker 镜像大小

初始 RHEL 图像大小 = 196M

Dockerfile - 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

Dockerfile - RHEL Image(+bc) with "yum clean all"= 207M 节省 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”输出中的建议

关于docker - 如何减小 RHEL/Centos/Fedora Docker 镜像的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46089219/

相关文章:

php - docker 上带有 Nginx、php 7.4 fpm 和 mysql 8 的 Laravel 6 比 php 7.1 上的 Laravel 4 慢

docker - Fn 项目缺少 http 操作 (CRUD)

centos - 在 CENTOS 命令中删除多个文件

linux - 监视/var/log/messages 文件中的特定关键字

docker - 在 ubuntu 20.04 上部署 docker 容器到 swarm 时出现 br_netfilter 错误

linux - 从 BitBucket pull 到服务器使文件组权限为 0 0

通过 ansible 使用模式时 sed 找不到文件

php - 编译安装php5.4,make : *** [libphp5. la] 错误1

installation - 如何在 Redhat Enterprise Linux 6.0 上安装 pysvn?

docker - $'\r' : command not found with Docker Compose