Docker: Layers and Caching
April 18, 2025
1 min read
0 views
Why does your Docker build take 10 minutes? Because you are ignoring layers.
The Cake Analogy
A Docker image is like a cake with layers. 1. Ubuntu Base 2. Python Install 3. Requirements Install 4. Code Copy
If you change Layer 4 (Code), Docker re-uses Layers 1-3. Instant build! But if you swap Layer 3 and 4...
Bad Dockerfile:
Dockerfile
COPY . .
RUN pip install -r requirements.txt
Every time you change a code file (. changes), looking at Layer 1 implies Layer 2 needs to re-run. pip install runs every time. ðŸ˜
Good Dockerfile:
Dockerfile
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
Now, pip install only runs if requirements.txt changes.
Conclusion
Order matters. Put the things that change least (OS, Dependencies) at the top. Put the things that change most (Code) at the bottom.
Similar Posts
Docker: Init Systems (PID 1)
Jul 11, 2025