Join our Discord Server
Adesoji Alu Adesoji brings a proven ability to apply machine learning(ML) and data science techniques to solve real-world problems. He has experience working with a variety of cloud platforms, including AWS, Azure, and Google Cloud Platform. He has a strong skills in software engineering, data science, and machine learning. He is passionate about using technology to make a positive impact on the world.

How I Reduced Docker Image Size by 90%

3 min read

In today’s fast-paced DevOps landscape, every second counts. Smaller Docker images mean quicker builds, faster deployments, reduced bandwidth costs, and enhanced security. At Collabnix, we believe that optimizing your Docker images isn’t just about saving space—it’s about creating a robust, efficient, and agile deployment pipeline. Let’s dive into some practical strategies to help you create lean, mean containers.

Table of Contents

  1. Why Reducing Docker Image Size Matters
  2. Choose a Minimal Base Image
  3. Embrace Multistage Builds
  4. Skip Unnecessary Dependencies
  5. Leverage .dockerignore
  6. Optimize Dockerfile Layers
  7. Clean Up After Package Installation
  8. Opt for Smaller Language Runtimes
  9. Compress Image Layers
  10. Strip Out Debug Information
  11. Regularly Audit Your Images
  12. Advanced Optimization Tips
  13. Conclusion

1. Why Reducing Docker Image Size Matters

Streamlining your Docker images is more than just a neat trick—it’s a necessity. Smaller images lead to:

  • Faster Builds: Minimized image sizes speed up your build and deployment cycles.
  • Cost Efficiency: Less storage space and lower bandwidth usage translate to reduced costs.
  • Rapid Container Startups: Quick startups are essential in environments that need rapid scaling.
  • Enhanced Security: Fewer components mean fewer potential vulnerabilities.

2. Choose a Minimal Base Image

Every Docker image starts with a base image. Opting for a minimal base can make a huge difference. Consider these options:

Alpine Linux

A popular, lightweight option at around 5MB. (Note: Some dependencies might need extra work.)

FROM alpine:3.18

Distroless Images

Designed for production, these images contain only the essentials needed to run your application.

FROM gcr.io/distroless/base

3. Embrace Multistage Builds

Multistage builds allow you to separate the build environment from your final image. This approach means you only ship what’s necessary for production.

# Stage 1: Build
FROM golang:1.19 AS builder
WORKDIR /app
COPY . .
RUN go build -o main .

# Stage 2: Production
FROM alpine:3.18
WORKDIR /app
COPY --from=builder /app/main /app/
CMD ["./main"]

4. Skip Unnecessary Dependencies

Keep your Docker images lean by installing only what your application truly needs. When using package managers like apt-get, avoid pulling in extra, non-essential packages.

RUN apt-get update && apt-get install --no-install-recommends -y \
    curl \
    ca-certificates \
 && rm -rf /var/lib/apt/lists/*

5. Leverage .dockerignore

Similar to a .gitignore file, a .dockerignore file ensures that extraneous files aren’t sent to the Docker build context. This not only reduces build time but also keeps your images clutter-free.

node_modules
.git
.env
tmp/
logs/

6. Optimize Dockerfile Layers

Every instruction in a Dockerfile adds a layer to your image. Combining commands can minimize the number of layers and help eliminate leftover temporary files.

Before Optimization:

RUN apt-get update
RUN apt-get install -y python3
RUN apt-get clean

After Optimization:

RUN apt-get update && apt-get install -y python3 && apt-get clean

7. Clean Up After Package Installation

During the build process, package managers often leave behind caches and temporary files. Cleaning these up is vital to maintaining a compact image.

For Debian-based images:

RUN apt-get update && apt-get install -y python3 && apt-get clean && rm -rf /var/lib/apt/lists/*

For Alpine-based images:

RUN apk add --no-cache python3

8. Opt for Smaller Language Runtimes

Many programming languages offer slim or Alpine versions of their runtime images. Switching to these can trim down your image significantly.

# Instead of:
FROM python:3.11

# Use the slim version:
FROM python:3.11-slim

9. Compress Image Layers

While Docker compresses layers by default, you can further optimize by manually compressing files (e.g., using gzip or tar) before including them in your image. This is particularly useful when dealing with large binaries or package archives.

compress

10. Strip Out Debug Information

Debug symbols and metadata can bloat your application binaries. Removing this extra information, especially in production, can save precious space.

RUN strip /path/to/binary

11. Regularly Audit Your Images

Over time, even the best images can accumulate unnecessary components. Regularly cleaning up your Docker environment using commands like docker image prune can keep your system tidy.

docker image prune -f

Docker also offers an experimental --squash flag that can combine multiple layers into one, though its usage depends on your project’s requirements.


12. Advanced Optimization Tips

  • Docker Image Scanning: Security is paramount. Tools like Docker Scout, Trivy, or Clair can help you identify vulnerabilities and outdated packages, offering insights into further optimization.
  • OverlayFS & Shared Layers: In environments like Kubernetes, utilizing shared layers via OverlayFS can reduce disk usage by ensuring that only differences between layers are stored.
  • Exploring Unikernels: For projects where every byte counts, consider unikernels. These specialized, single-purpose virtual machines package only the application and its essential OS components, offering unmatched minimalism—albeit with added complexity.

13. Conclusion

Optimizing Docker image size is more than a technical exercise—it’s a commitment to efficiency, speed, and security. By choosing minimal base images, harnessing the power of multistage builds, and meticulously cleaning up after package installations, you can dramatically reduce your image sizes. Regular audits and advanced techniques ensure that your containers remain agile and production-ready.

At Collabnix, we’re passionate about sharing best practices that empower you to build smarter, faster, and leaner applications. Embrace these strategies, and watch your deployment pipelines transform into efficient, streamlined workflows.

Happy containerizing!


For more information, feel free to reach out to us at collabnix@gmail.com.

Sculpting Slim Containers

Have Queries? Join https://launchpass.com/collabnix

Adesoji Alu Adesoji brings a proven ability to apply machine learning(ML) and data science techniques to solve real-world problems. He has experience working with a variety of cloud platforms, including AWS, Azure, and Google Cloud Platform. He has a strong skills in software engineering, data science, and machine learning. He is passionate about using technology to make a positive impact on the world.

Snowflake, Model Context Protocol (MCP) Server and Claude Desktop

  TL;DR: We’ll set up and run a Model Context Protocol (MCP) server that communicates with Snowflake to run SQL queries. We’ll install using...
Adesoji Alu
3 min read

Leave a Reply

Join our Discord Server
Index