【问题标题】:How to reduce the size of RHEL/Centos/Fedora Docker image如何减小 RHEL/Centos/Fedora Docker 镜像的大小
【发布时间】:2018-02-15 18:30:25
【问题描述】:

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

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

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

【问题讨论】:

    标签: docker centos redhat fedora


    【解决方案1】:

    是的,通过执行“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”输出中的建议

    【讨论】:

      【解决方案2】:

      除了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

      【讨论】:

      • 绝对正确,甚至选择像 Alpine 这样的东西。归根结底,我怀疑 yum clean all 也适用于那些。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-18
      • 1970-01-01
      • 2018-12-21
      • 2019-07-11
      • 1970-01-01
      相关资源
      最近更新 更多