Back to articles
How I reduced a Docker image from 1.12 GB to 131 MB (88% smaller)

How I reduced a Docker image from 1.12 GB to 131 MB (88% smaller)

via Dev.to PythonNadir

When I started working with Docker, I made the same mistake most beginners make — I used the full base image and copied everything into the container without thinking about size or performance. The result? A 1.12 GB image for a simple Flask app. Here's exactly what I changed to bring it down to 131 MB. The bad Dockerfile (what most tutorials show you) FROM python:3.11 WORKDIR /app COPY . . RUN pip install -r requirements.txt CMD ["python", "app.py"] Problems with this: python:3.11 is ~950 MB — includes compilers and tools your app will never use COPY . . before installing dependencies kills layer caching — pip reinstalls everything on every single code change No .dockerignore — copies your .git folder, __pycache__ , and other junk into the image Flask dev server is not suitable for production The optimized Dockerfile FROM python:3.11-slim AS builder WORKDIR /app COPY requirements.txt . RUN pip install --no-cache-dir --user -r requirements.txt FROM python:3.11-slim WORKDIR /app COPY --f

Continue reading on Dev.to Python

Opens in a new tab

Read Full Article
0 views

Related Articles