How To Optimize Docker Image

Technext
4 min readOct 23, 2022

--

How To Optimize Docker Image

A Docker image is a file that is responsible for executing code in a docker container. It keeps sort of instruction to build docker containers. A Docker image contains application code, libraries, tools, dependencies, and other files needed to make an application run. Docker images have multiple layers and this is responsible for increasing reusability and decreasing disk uses. Images are read-only but once the container is created a layer is added on top of the unchangeable image. There are two methods to create a docker image.

  1. Interactive method
  2. Dockerfile method

The following are the methods by which we can achieve docker image optimization.

  1. Using distroless/minimal base images: So firstly you need to choose a minimal base image with a minimal OS footprint, like- Alpine images can be as small as 5.59MB. You can further reduce the base image size using distroless images.
  2. Multistage builds: In this method, we can use different Dockerfiles for building and packaging the application code. We use intermediate images to compile code, install dependencies, and package files in the approach. For each instruction in Dockerfile, it creates a layer in the image. So we need to clean up unwanted artifacts before moving into the next layer. So you need to use shell tricks or other logic to keep the layer small.

To use multistage, we need to use FROM statement multiple times. Here is an example:

How To Optimize Docker Image

3. Minimizing the number of layers

Docker images work in the following way — each RUN, COPY, and FROM.

In stage one, it builds and creates the deployable file and then in stage two, it copies all required files from the previous build file. Before we were told that each Dockerfile instruction creates a new layer and each layer builds the execute time and increases storage.

We can see an example here:

Non-Optimize:

How To Optimize Docker Image — Non Optimize

Optimize:

How To Optimize Docker Image — Optimize

Here we just write one RUN command and it creates only one layer.

4. Understanding caching

Sometimes we are new to rebuilding the same image again and again. Docker helps this case by using caching.

Non-Optimize:

How To Optimize Docker Image — Non Optimize

Optimize:

How To Optimize Docker Image — Optimize

It’s recommended to add instructions which use for installing packages or dependencies before the copy command. The reason behind this is that docker would be able to cache the image with the required dependencies, and this cache can then be used in the following builds when the code gets modified.

5. Using Dockerignore

As the rule, only the required file needs to be copied over the docker image. Docker can ignore the file that keeps in the .dockerignore file. So this feature needs to be kept in mind to optimize the docker image.

6. Keeping application data elsewhere

Storing application data into the image causes an increase in its size of it. It’s recommended to use the volume feature of container runtime to keep the data separate from the image.

We can use Dive or Docker slim tools to optimize the docker image. Those are open source.

Credit

This post was written by one of the best software engineers at Technext — Sakin Hossain.

Technext helps startups and businesses hire great software engineers.

You can hire an offshore software development team from Technext.

Please visit the services we offer as offshore software development services for your startups.

Have any projects?

Reach out to hire the brilliant software engineering and UI/UX design team. Build your dream projects.

Let’s collaborate

Speak with our leadership team directly.

https://bd.linkedin.com/in/syed-rezwanul-haque-b7889628

--

--

Technext

Technext is an offshore software development, UI/UX design and digital company