📝 Docker

Layers and Caching in Docker

0
Author
04e5cc8b-58ac-4bdc-bdee-661bbb
📅
Published
08.05.2026
⏱️
Reading time
1 min
👁️
Views
62
🌱
Level
Beginner

Every instruction in a Dockerfile creates a layer — an intermediate image. Docker caches layers and reuses them on subsequent builds if the instruction and its inputs haven’t changed.

How the Cache Works

When building, Docker checks each instruction:
- If the instruction and its inputs haven’t changed — uses the cache (instant)
- If something has changed — rebuilds that layer and all subsequent ones

Instruction Order Matters

Bad order — any code change triggers a full dependency reinstall:

FROM python:3.11-slim
WORKDIR /app
COPY . .                          # ← copies everything at once
RUN pip install -r requirements.txt  # ← rebuilds on every code change!
CMD ["python", "app.py"]

Correct order — dependencies are only rebuilt when requirements.txt changes:

FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .           # ← copy only requirements.txt first
RUN pip install -r requirements.txt  # ← cached if requirements didn't change
COPY . .                          # ← copy code after dependencies
CMD ["python", "app.py"]

Rule: order instructions from least frequently changed to most frequently changed.

Inspecting Layers

# Show image layers
docker history myapp:v1

# Detailed layer information
docker inspect myapp:v1

Cache Invalidation

# Build without cache
docker build --no-cache -t myapp:v1 .

# Force rebuild from a specific step — change the instruction or add an ARG:
ARG CACHEBUST=1
RUN apt-get update

Multi-stage Builds — Shrinking the Final Image

The build stage contains compilers and tooling. The final stage contains only the output.

# Build stage
FROM python:3.11 AS builder
WORKDIR /app
COPY requirements.txt .
RUN pip install --user -r requirements.txt

# Final image
FROM python:3.11-slim
WORKDIR /app
COPY --from=builder /root/.local /root/.local
COPY . .
CMD ["python", "app.py"]

The final image contains no pip, no compilers, no intermediate files — only what’s needed to run.

Image Size Tips

  • Use slim or alpine base images
  • Combine multiple RUN apt-get install calls into a single instruction
  • Clean up the package manager cache: && rm -rf /var/lib/apt/lists/*
  • Use .dockerignore
  • Use multi-stage builds for compiled languages

Your reaction to the article

💬 Comments (0)

🔐 Sign in to leave a comment
🚪 Login
💭

No comments yet

Be the first to share your opinion about this article!

🔗 Similar

Similar articles

Continue learning with these materials

📝

Docker Hub — Image Registry

Docker Hub is the public Docker image registry. Think of it as npm for Node.js...

📅 08.05.2026 👁️ 49
📝

Docker Compose: Multi-Container Applications

Docker Compose is a tool for running multiple related containers as a single application. The...

📅 08.05.2026 👁️ 49
📝

Installing Docker: macOS, Windows, Linux

Download Docker Desktop from docker.com/products/docker-desktop.

📅 08.05.2026 👁️ 45

Did you like the article?

Subscribe to our updates and receive new articles first. Grow with PyLand!