5 Easy Steps to Minimize Docker Image Size

5 Easy Steps to Minimize Docker Image Size

·

4 min read

Introduction:

Reducing the docker images helps in speeding up the container deployment. In large scale deployments the reduced image size may result in significantly saving storage space. Here, we will explore few tips for minimizing docker images while retaining the necessary data.

Let’s Explore

Before understanding the steps, let us see how to check the docker image size ?

To check the size of the image, use the following command to display the information.

docker images

How to reduce the Docker Image Size

Steps:

  1. Use a Minimal Base Image

Selecting the base image is one of the most important factor to reduce the Docker image size. Minimal base image such as Alpine, Scratch or Slim are significantly smaller than larger base image such as ubuntu or debian as they come with only the essentials which are required to run the application.

Image variants

  • node : <version>

This is the default image which usually has the complete operating system with all the system libraries and dependencies. This image is larger in size. (~1 GB).

  • node : <version> - slim

This is a minimal version of the full image, which uses debian as the base operating system but with fewer packages. This image is smaller compared to the default image. (~150 MB).

  • node : <version> - alpine

This is a lightweight image based on alpine linux. This is mostly used in production stage where faster deployment are priorities. The size of this image is very small compared to the other variants. (~100 MB)

Distroless Images: A distroless image is very minimalistic or lightweight docker image. These image contain a minimal Linux, Nodejs based runtime.

  • Example with Distroless
FROM gcr.io/distroless/nodejs18-debian12

Image size is 115 MB.

  • Example with Slim
FROM node:18-slim

Image size is 192 MB.

  • Example with alpine
FROM node:18-alpine

Image size is 127 MB.

  1. Multistage Builds

Multistage builds allows us to separate the build environment from the runtime environment. This ensures that only the essential file goes to the final stage. This helps in reducing the final docker image by excluding the build tools and dependencies that are not needed in the runtime.

Example with Single Stage

FROM node:18
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["node", "index.js"]

Example with Multi Stage

#Stage 1
FROM node:18 AS build
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .

#Stage 2
FROM node:18-alpine
WORKDIR /app
COPY --from=build /app/package*.json ./
RUN npm install --only=production
COPY --from=build /app ./
EXPOSE 3000
CMD ["node", "index.js"]

With Single Stage Builds: The final image would include all the build dependencies and the application code.

With Multi Stage Builds: The final image would contain only runtime dependencies (which is needed to run the app) and application code.

  1. Avoid Unnecessary Layers

In a Dockerfile, each RUN creates a new layer in the final image. You can combine multiple operations into a single RUN command to reduce the number of layers and keep the image smaller.

Pro tip:

Minimize the layers by combining RUN commands. This creates one single layer.

Place the frequently changing commands like COPY at the end of the Dockerfile to optimize the build cache.

Example with Multiple RUN commands

FROM node:18
RUN apt update 
RUN apt install nginx -y 
RUN apt install curl -y

Example with Single RUN command.

FROM node:18
RUN apt update && apt install nginx -y && apt install curl -y
  1. Use of .dockerignore File

The .dockerignore file is similar to .gitignore file, but this is related to docker build. This file defines which files and directories should be excluded during the build process. By excluding unnecessary files, we optimize the build process resulting in faster and more efficient builds.

Example with .dockerignore

Create a .dockerignore file

node_modules
.git
.env
tmp/

We have excluded the above files from getting build. This will result in speeding up the build process.

  1. Remove Unnecessary Packages

When installing software avoid installing optional dependencies that you do not need. Use - - no-install-recommends flag

Example:

RUN apt-get update && apt install --no-install-recommends -y

This will install only the core packages without suggested or recommended extras.

Please remember that the effectiveness of these steps may vary depending upon the specific requirements and nature of your application. By regularly auditing and refining your docker image, you can ensure that your containers are lean, secure and production ready.

Conclusion:

By applying all the above steps, you can optimize your Docker image and keep them small to improve your application performance. Small images are not just faster to deploy but they also contribute for better security, reliability and efficiency in cloud native environment.

If you found this post useful, give it a like👍

Repost♻️

Follow Bala for more such posts 💯🚀