Docker

Does Docker squash Reduce Image Size?

Docker image is an essential component of Docker. The Docker image is referred to as a file that contains instructions to execute the Docker container. Additionally, Docker images are utilized to share or run the application on another machine. Without a Docker image, the developers are unable to deploy the project.

This blog will demonstrate does Docker squash reduces image size.

Does Docker Squash Reduce the Image Size?

Yes, the Docker squash reduces the image size. Docker squash is one of the famous Docker utilities used to build smaller size Docker images by squashing the Docker multiple layers into one fewer layer.

To utilize the Docker squash, follow the below-listed methods:

Prerequisite: Enable Docker Daemon Experimental Feature

To run the Docker squash to build the Docker image with fewer layers, users are required to enable the experimental feature of Docker Daemon. Otherwise, during the execution of the Docker squash command, the user may get the error “–squash is only supported on Docker daemon with experimental features enabled” as shown below:

How to Experimental Features of Docker Daemon?

To enable the experimental feature of Docker Daemon, go through the provided instructions.

Step 1: Open Docker Application

Open the Docker Desktop application from the Windows “Startup” menu:

Step 2: Open Docker Settings

Next, open the Docker settings by clicking on the “Gear” icon:

Step 3: Enable Experimental Feature

Open the “Docker Engine” settings. Here, you can see the value of the experimental feature is set as “false” which means currently the experimental features are disabled:

To enable the experimental features of the Docker daemon, set its value as “true”. After that, hit the “Apply & restart” button to save the changes:

How to Use Docker Squash?

To use the Docker squash to reduce the Docker image size, look at the given instructions.

Step 1: Open Visual Studio Code

First, open the Visual Studio Code editor from the Windows Start menu:

Step 2: Open Dockerfile

Open the Dockerfile through which you want to build a Docker image. For instance, we have opened the Dockerfile that will execute the simple Python code:

Step 3: Launch Terminal in VS Code

After that, launch the terminal from the “Terminal” menu as highlighted below:

From the below-highlighted drop-down menu, users can switch to their favorite terminal. In our case, we are using the “Git Bash” terminal:

Step 4: Create an Image From Dockerfile

To create a new image from Dockerfile without squashing image layers, utilize the simple “docker build -t <image-name> -f Dockerfile .” command:

$ docker build -t demo -f Dockerfile .

For instance, we have created the “demo” Docker image:

Step 5: Check Docker Image

Confirm if the new image is created or not by utilizing the “docker images” command:

$ docker images

It can be observed that the Docker image “demo” is created, and the size of the image is “940MB”:

Step 6: Run Docker Squash

Now, execute the “Docker build –squash” command to build the new image with the same name but with fewer Docker layers:

$ docker build --squash -t demo .

Again, check the Docker images to verify whether the image size is reduced or not:

$ docker images

It can be seen that the size of the newly created image is reduced to “929MB”:

We have demonstrated how Docker squash reduces the image size.

Conclusion

Yes, Docker squash can reduce images by squashing Docker layers. Users must activate the Docker daemon experimental functionality in order to use Docker squash. After that, run the “Docker build –squash -t <imagename> .” command to reduce the size of the Docker image. This write-up has elaborated on whether Docker squash reduces image size.

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.