来自 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/