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

Yes Docker image sizes can be dramatically reduced by doing a “yum clean all”

Initial RHEL Image Size = 196M

Dockerfile – RHEL Image(+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 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

NOTE: The –squash option comes as an experimental flag in the latest version of Docker. It compresses the layered file system into a single new layer https://blog.docker.com/2017/01/whats-new-in-docker-1-13/

I found the solution of using “yum clean all” at https://medium.com/@vaceletm/docker-layers-cost-b28cb13cb627

The addition of “rm -rf /var/cache/yum” comes from the suggestion in the output of the “yum clean all”

Leave a Comment