Thumbnail image

Why You Should Optimize Your Docker Images

Key Concept of Docker

Before discussing the optimization of Docker image files, let’s briefly introduce the purpose and origin of Docker.

The popularity of Docker stems from its ability to standardize the running versions of applications. The pain point it aims to solve is that the project environment can run smoothly on any computer, in any environment, with just a few commands, without the need for complicated preliminary work.

3 things you need to know in Docker:

  • Registry: A space for storing Docker image files, similar to services like GitHub, for example: DockerHub, Google Container Registry.
  • Image: A read-only environment that defines the settings required for running an application; this environment can be defined through a Dockerfile.
  • Container: An executable process generated from the definition of an Image.

Dockerfile :

A Dockerfile can effectively define the environment required for developers to run an application. It is also necessary to use Docker commands to create a Docker Image from a Dockerfile; thus, the way a Dockerfile is written can somehow affect the size of the Docker image.

The following methods can reduce the size of a Docker image, with Java as an example:

  • Use Multi-Stage Builds: Separate the process of building the source code from the process of executing the runnable files.
  • Use a Smaller Base Image: You can use smaller images such as Ubuntu or Alpine as the base to create your image.
  • Include Only Necessary Files and Content: For Java, for example, the runtime environment does not need development tools, so we use JRE as the application runtime environment instead.

Here is also the official guideline for Dockerfile provided for reference.

Comparisons :

## total Size Base Image Size
JDK 457 MB 210.63 MB
JRE 176 MB 120 MB

From the table above, we can summarize the following information:

The image size using JDK is larger than that using JRE because JDK includes developer tools. Therefore, for production environments, JRE should be the primary base.

Useful Resource: