Resourses Blog How to Reduce…

How to Reduce Docker Image Size

How to Reduce Docker Image Size

Recently, I have been tasked to migrate the existing set of Docker images from Ubuntu to RHEL UBI. The product has more than 25 images, so keeping the new image size as small as possible is one of the goals while migrating the images. Everyone is well aware of the advantages of keeping the Docker image size small for the following reasons.

  • Larger images take time to download from container registry to host machine or Kubernetes cluster
  • Larger images take up more disk space on the host machine
  • Larger images usually contain code which is not used during image run

In all, it’s a time-consuming task, however, by the end of this blog post, I’ll ensure this process to be quick and easy. I have penned down the process and kept it to as minimal as possible, follow the given technique I have used to cut down the size of the new image:

  • Usually, it is recommended to use the smaller size base image. However, I have limited choice as,
    • UBI ( ~ 202 MB )
    • UBI minimal ( ~ 76 MB )
  • Used the standard techniques like below.
    • yum update is strictly avoided
    • Installed the only required packages within the image
    • Clubbed all the RUN instructions in a single layer
RUN yum -y install curl git && \

         wget http:// …
  • Used the multi-stage builds. This made the new image really lean. All the application code is compiled in the UBI container and only the final binaries are copied to UBI-minimal container. For example, rsync is complied with and copied as below.
FROM registry.access.redhat.com/ubi7/ubi:7.6 AS intermediate-image

RUN wget https://www.samba.org/ftp/rsync/src/rsync-3.1.3.tar.gz -o /tmp && \

         tar -xzvf /tmp/rsync-3.1.3.tar.gz && \

         cd /tmp/rsync-3.1.3 && \

         ./configure && \

         make && \

         make install

FROM registry.access.redhat.com/ubi7/ubi-minimal:7.6

COPY -- from=intermediate-image /usr/bin/rsync /usr/bin

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.

Docker Image Reduce

And that’s exactly how you reduce the size of Docker images! I’m sure there are other techniques that I haven’t explored because this one is effortless! However, let me know in the comments section if you have an efficient way of reducing the Docker image size.

Author:

Ameya Varade

Ameya is a cloud computing specialist with more than 12years in the field. At CloudHedge, he drives technical leadership with an aptitude in architecting complex systems, leads niche technical teams, while ensuring a customer-focused approach.

View All Posts

Our Solutions

  • Related Article

    Automated App Modernization of Apps for a Leading European Vehicle Manufacturer to AWS
    Read...
    Close
    ×