代码之家  ›  专栏  ›  技术社区  ›  TJA

如何减小RHEL/Centos/Fedora Docker图像的大小

  •  23
  • TJA  · 技术社区  · 7 年前

    Red Hat的基础图像非常小,对于RHEL 7.4来说大约为196M。然而,它往往缺少我想要为其构建新图像的产品所需的许多细节。

    2 回复  |  直到 7 年前
        1
  •  46
  •   TJA    7 年前

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

    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图像(+bc),“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
    

    https://blog.docker.com/2017/01/whats-new-in-docker-1-13/

    我找到了使用“yum clean all”的解决方案 https://medium.com/@vaceletm/docker-layers-cost-b28cb13cb627

    添加“rm-rf/var/cache/yum”来自“yum clean all”输出中的建议

        2
  •  4
  •   user2915097    7 年前

    除了TJA的答案之外,您还可以使用更小的CentOS基图像,例如,存在Debian灯光,它被称为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