• TechOps Examples
  • Posts
  • How I Reduced Docker Image Size from 588 MB to Only 47.7 MB

How I Reduced Docker Image Size from 588 MB to Only 47.7 MB

In partnership with

TechOps Examples

Hey — It's Govardhana MK 👋

Welcome to another technical edition.

Every Tuesday – You’ll receive a free edition with a byte-size use case, remote job opportunities, top news, tools, and articles.

Every Thursday and Saturday – You’ll receive a special edition with a deep dive use case, remote job opportunities and articles.

AI agents are easy to prototype. Hard to scale.

You end up with fragile pipelines, manual configs, and unpredictable deployments. Every “fix” just adds another layer of duct tape.

Join our hands-on Kagent Workshop (with Solo.io, PerfectScale by DoiT, and the Kagent Community) to learn how to deploy, configure, and run AI agents the Kubernetes way.

Learn how to:

  • Install and deploy Kagent

  • Configure ModelConfigs + MCP servers

  • Build intelligent agents that scale

  • Automate event-driven execution

PS! No more chaos. Just declarative, scalable automation.

IN TODAY'S EDITION

🧠 Use Case
  • How I Reduced Docker Image Size from 588 MB to Only 47.7 MB

👀 Remote Jobs

📚️ Resources

TOGETHER WITH MINDSTREAM

Master ChatGPT for Work Success

ChatGPT is revolutionizing how we work, but most people barely scratch the surface. Subscribe to Mindstream for free and unlock 5 essential resources including templates, workflows, and expert strategies for 2025. Whether you're writing emails, analyzing data, or streamlining tasks, this bundle shows you exactly how to save hours every week.

If you’re not a subscriber, here’s what you missed last week.

To receive all the full articles and support TechOps Examples, consider subscribing:

🧠 USE CASE

How I Reduced Docker Image Size from 588 MB to Only 47.7 MB

To begin with, there is no secret here if you already know about the multi stage builds. We all know minimizing docker image sizes accelerates container deployment, and for large scale operations, this can lead to substantial savings in storage space.

1) For a flask app, I picked up Python 3.9-alpine which is a whomping 95.2% smaller than Python 3.9

This minimal images contain only the essentials, significantly reducing the image size.

2) I minimized layers - every command in a Dockerfile (like RUNCOPY, etc.) generates a separate layer in the final image. Grouping similar commands together into one step makes sense, which decreases the total number of layers, leading to a smaller overall image size.

Instead of doing this:

RUN apk update
RUN apk add --no-cache git
RUN rm -rf /var/cache/apk/RUN apk update
RUN apk add --no-cache git
RUN rm -rf /var/cache/apk/*  *

Do this:

RUN apk update && apk add --no-cache git && rm -rf /var/cache/apk/*

3) Used .dockerignore File - Docker transfers all the files from your project directory into the image by default. To avoid including unneeded files, used a .dockerignore file to exclude them.

__pycache__
*.pyc
*.pyo
*.pyd
venv/

4) Multi Stage Builds (Mandatory atleast for me)

Single Stage Vs Multi-Stage Builds Comparison:

Take an example of a Flask app built using the python:3.9-alpine image with a single-stage Dockerfile

The image built was of size: 588 MB

Redesigned Multi Stage Dockerfile looks like:

# Dockerfile.multi-stage

# Stage 1: Build
FROM python:3.9-alpine AS builder

# Install necessary build dependencies
RUN apk add --no-cache build-base \
    && apk add --no-cache gfortran musl-dev lapack-dev

# Set the working directory
WORKDIR /app

# Copy the requirements file and install dependencies
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

# Copy the rest of the application code to the working directory
COPY . .

# Uninstall unnecessary dependencies
RUN pip uninstall -y pandas && apk del build-base gfortran musl-dev lapack-dev

# Stage 2: Production
FROM python:3.9-alpine

# Set the working directory
WORKDIR /app

# Copy only the necessary files from the build stage
COPY --from=builder /app /app

# Expose the port the app will run on
EXPOSE 5000

# Run the Flask app
CMD ["python", "app.py"]

The new image size was: Only 47.7 MB

The application works exactly the same, but it spins up much faster in this version. That's an whomping -91.89 %

Less the image size = Faster deployments + Quicker scaling + Lean infrastructure

🔴 Get my DevOps & Kubernetes ebooks! (free for Premium Club and Personal Tier newsletter subscribers)

Looking to promote your company, product, service, or event to 55,000+ DevOps and Cloud Professionals? Let's work together.