Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

The base image from Red Hat is quite small, on the order of 196M for RHEL 7.4. However it tends to be missing a lot of the bits and pieces that are required by the products I want to build new images for.

The moment I do a "yum install Xxx" on top of it the image size blows out to by +500M-800M.

Is there a way to reduce the size of the image?

like image 577
TJA Avatar asked Sep 07 '17 06:09

TJA


People also ask

Why is Docker image size so large?

With Docker, it's very easy to unknowingly bloat your image. Every command creates a new layer, and all layers are saved separately. Therefore, if a big file is generated by one command and removed later in the Dockerfile, it will still add bloat to the size.

Does size of Docker image matter?

Total image size does not matter. Base image size does not matter. Thus, reducing base image size in an attempt to reduce total disk consumption is meaningless (except in some borderline cases when the disk is unrealistically small).

Does Docker squash reduce image size?

Used Docker Squash to reduce the size of the final image. This is effective if your image has multiple layers created using RUN clause. The Squash attempts to create a single layer out of all the layers and thus reduced the overall size. I did get the size down up to ~12% for a few images.


2 Answers

Apart from TJA's answer, you can also use a smaller CentOS base image, for example, a Debian light exists, it is called Bitnami

https://hub.docker.com/r/bitnami/minideb-extras/

For CentOS maybe you can use

https://hub.docker.com/r/blalor/centos/

You can also try to reduce the size of your images using 2 tools

https://github.com/mvanholsteijn/strip-docker-image

and

https://github.com/docker-slim/docker-slim

like image 27
user2915097 Avatar answered Oct 10 '22 17:10

user2915097


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"

like image 68
TJA Avatar answered Oct 10 '22 19:10

TJA