Docker

How to Trim a Docker Image?

Docker images are also known as a snapshot or templates of Docker containers. It is the major component of Docker that is utilized to build, execute and manage containers. It also instructs the containers on how to containerize the software or project. The Docker images are usually huge in size as it contains all the essential information about Docker dependencies, code, and configuration settings.

This blog will provide the methods to trim a Docker image:

Method 1: Trim Docker Image Using Lighter Base Image

To trip the Docker image, the one possible solution is to use the light base image for building the Docker image such as using the python image as a base image will create a heavier image. However, instead of the “python” image, if the user uses the ubuntu image as a base image, it will create a lighter image relative to the “python” image.

For the demonstration, follow the following steps.

Step 1: Create Dockerfile

First, create the Docker image to containerize the simple Python code. For instance, we have utilize the following Dockerfile commands:

  • FROM” statement is used to define the base image. For demonstration, we have utilized the “python” image as the base image.
  • RUN” is a Dockerfile command that executes the stated commands. Here, the RUN command is installing some tools like python-setuptools, pip, dev, and git.
  • EXPOSE” is used to define the container’s exposed port.
  • CMD” statement is defining the executables for Docker containers:
FROM python
RUN apt-get update && apt-get install -y --no-install-recommends \
        python3-pip \
        python3-dev \
        python3-venv \
        git \
        && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*
EXPOSE 8000
CMD python3 -c "print('Docker is more simple Deployment Tool')"

 

Step 2: Generate the New Docker Image

Next, generate the Docker image from Dockerfile instructions through the given command. The “-t” option is specifying the name of the image:

docker build -t py-img .

 

Step 3: View Image Size

To view the size of the image, use the “docker images <image-name>” command:

docker images py-img

 

From the output, you can see that the image size is “957MB” which is huge to print simple lines through Python code:

Let’s reduce the image size by using the lighter base image.

Step 4: Use Lighter Base Image

Now, in the same Dockerfile, change the base image by updating the “FROM” statement. For instance, we have replaced the “python” with the “ubuntu:latest” image in the “FROM” statement. The remaining commands or statements will not change:

FROM ubuntu:latest

RUN apt-get update && apt-get install -y --no-install-recommends \
        python3-pip \
        python3-dev \
        python3-venv \
        git \
        && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

EXPOSE 8000

CMD python3 -c "print('Docker is more simple Deployment Tool')"

 

Next, apply the changes through the “CTRL+S” key. Let’s move forward to recreate the Docker image.

Step 5: Rebuild the Image

Now, rebuild the Docker image by utilizing the given command:

docker build -t py-img .

 

Step 6: Check Image Size With Lighter Base Image

Again, check the size of the image using the “docker images” command:

docker images py-img

 

The output shows that the size of the image is reduced from “957MB” to “243MB”:

Method 2: Trim Docker Image by Uninstalling or Removing Extra Dependencies

To trim the size of the Docker image, eliminate the extra dependencies and libraries from the Docker image such as in the above-given example, the python-setuptools, pip, dev, and git are extra dependencies that are not required to execute the simple python print code. For this purpose, go through the mentioned steps.

Step 1: Create Dockerfile

Let’s use the previous Dockerfile for demonstration. Open the Dockerfile and remove the extra dependencies like python-setuptools, pip, dev, and git as shown below:

FROM ubuntu:latest
RUN apt-get update && apt-get install -y --no-install-recommends \
  && apt-get install -y python3 \
  && rm -rf /var/lib/apt/lists/*
EXPOSE 8000
CMD python3 -c "print('Docker is more simple Deployment Tool')"

 

Let’s move ahead to generate the new image.

Step 2: Generate Docker Image

Generate the Docker image by utilizing the mentioned command:

docker build -t py-img .

 

Step 3: Check Image Size

Again, list down the images and check the image size:

docker images py-img

 

The output indicates that the image size is trimmed from “243MB” to “108MB”:

Method 3: Trim Docker Image Using Multistage Build

The multistage build is the process of building the image in multiple stages. The base stage or first stage contains the dependencies installation information and configuration settings. The sub-stages or child stages contain container executables. The multistage build reduces the image size.

For illustration, follow the listed steps.

Step 1: Create Multistage Dockerfile

First, create the Dockerfile and paste the following code into the file. In the below code, two “FROM” statements are used to define a two-stage build. The first “FROM” statement uses the “ubuntu:latest” image and this stage is also a base stage. The second FROM statement uses an “alpine” image and this stage is referred to as the child stage or sub-stage:

FROM ubuntu:latest AS builder
RUN apt-get update && apt-get install -y --no-install-recommends \
  && apt-get install -y python3 \
  && rm -rf /var/lib/apt/lists/*
EXPOSE 8000

FROM alpine
CMD python3 -c "print('Docker is more simple Deployment Tool')"

 

Step 2: Make the Docker Image

Next, generate the Docker image by utilizing the “docker build” command:

docker build -t py-img .

 

Step 3: View the Image Size

Next, check the size of the image through “docker images” command:

docker images py-img

 

Here, you can see the multistage build trims the Docker image, and the size is reduced from “108MB” to only “7.33MB”:

That’s all about trimming the Docker image.

Conclusion

To trip the Docker image, different techniques can be used such as multistage builds, removing extra dependencies and libraries, or using a lighter base image. These all modifications can be done by modifying the Dockerfile. After that, build the Docker image and check the size of the image through the “docker images” command. This post has demonstrated the methods to trim the Docker image.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.