Operations 15 min read

6 Proven Ways to Shrink Your Docker Image Size

Learn six practical techniques—including using minimal base images, multi‑stage builds, layer minimization, cache awareness, .dockerignore files, and external data storage—to dramatically reduce Docker image size, improve build speed, and lower security risks for containerized applications.

Raymond Ops
Raymond Ops
Raymond Ops
6 Proven Ways to Shrink Your Docker Image Size

How to Reduce Docker Image Size: 6 Optimization Methods

If you want to reduce the size of Docker images, you need to follow standard best practices for building Docker images.

This article discusses quick‑to‑implement optimization techniques for creating the smallest, most streamlined Docker images and introduces the best tools for Docker image optimization.

Docker Image Optimization Requirements

Although Docker’s build process is simple, many organizations mistakenly create bloated Docker images without optimizing them. As services evolve, each new version adds dependencies, commands, and configuration, making image builds larger and slower. Unnecessary libraries also increase attack surface and potential security risks, so DevOps engineers must keep images lean throughout CI/CD pipelines.

How to Reduce Docker Image Size?

A typical container image contains a base image, dependencies/files/configuration, and unwanted junk. Effective management of these resources is the key to optimization.

Below are six established methods for optimizing Docker images, each illustrated with practical examples.

Use distroless/minimal base images

Multi‑stage builds

Minimize the number of layers

Understand and leverage caching

Use .dockerignore to exclude unnecessary files

Store application data outside the image (volumes)

Method 1: Use Minimal Base Images

Choose a base image with the smallest OS footprint. For example, the Alpine base image can be as small as 5.59 MB and is also very secure.

`alpine       latest    c059bfaa849c    5.59MB`

Nginx on Alpine is only about 22 MB. Distroless images (available for Java, Node.js, Python, Rust, etc.) can further reduce size, though they lack a shell for debugging.

**Note:** Using publicly available base images may require approval from your organization’s security team.

Method 2: Use Docker Multi‑Stage Builds

Multi‑stage builds separate the build environment from the runtime image, eliminating unnecessary layers. An example builds the application in a node:16 stage, then copies only the needed files into a lightweight alpine runtime stage.

FROM node:16 as build
WORKDIR /app
COPY package.json index.js env ./
RUN npm install

FROM node:alpine as runtime
COPY --from=build /app /
EXPOSE 8080
CMD ["index.js"]

Building this Dockerfile yields an image of 171 MB compared to the original 910 MB—a reduction of over 80 %.

Method 3: Minimize Layers

Each RUN, COPY, or FROM instruction creates a new layer. Combining commands reduces the number of layers and storage requirements.

FROM ubuntu:latest
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update -y && \
    apt-get upgrade -y && \
    apt-get install --no-install-recommends vim net-tools dnsutils -y

Using this single‑layer approach reduces the image size from 227 MB to 216 MB and shortens build time.

Method 4: Understand Caching

Docker caches each layer; unchanged layers are reused in subsequent builds. Place commands that rarely change (e.g., installing dependencies) early in the Dockerfile to maximize cache hits.

**Tip:** Adding RUN or ADD after a COPY invalidates the cache for later layers.

Method 5: Use .dockerignore

Configure a .dockerignore file to exclude unnecessary files from the build context. This improves caching and prevents unwanted files from inflating the image.

Method 6: Store Application Data Elsewhere

Embedding application data inside the image unnecessarily increases size. Use Docker volumes (or Kubernetes PersistentVolumes) to keep data separate from the image.

Docker Image Optimization Tools

Here are some open‑source tools that can be integrated into your image pipeline:

1. Dive

图片
图片

Dive visualizes image layers and helps identify optimization opportunities.

2. SlimtoolKit

图片
图片

Docker Slim can shrink images dramatically (up to 1/30 of the original size) while improving security.

3. Docker Squash

图片
图片

Docker Squash compresses image layers to reduce overall size.

Summary

Applying these methods—choosing minimal bases, leveraging multi‑stage builds, reducing layers, using cache wisely, ignoring unnecessary files, and externalizing data—will help you build lean Docker images and write better Dockerfiles, ultimately achieving lightweight deployments.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Dockerimage-optimizationcachingmulti-stage-buildDockerfileDistroless
Raymond Ops
Written by

Raymond Ops

Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.