0
0
DockerHow-ToBeginner · 4 min read

How to Optimize Dockerfile for Caching to Speed Up Builds

To optimize Dockerfile caching, order instructions from least to most frequently changed and group commands to reduce cache invalidation. Use COPY and RUN wisely so Docker can reuse cached layers and speed up builds.
📐

Syntax

A Dockerfile consists of instructions like FROM, RUN, COPY, and CMD. Docker caches each instruction as a layer. If a layer's instruction or its inputs change, Docker rebuilds that layer and all following layers.

Optimizing caching means ordering instructions so that layers that change less come first, allowing Docker to reuse cached layers for faster builds.

dockerfile
FROM ubuntu:22.04
RUN apt-get update && apt-get install -y curl
COPY . /app
RUN make /app
CMD ["/app/start.sh"]
💻

Example

This example shows a Dockerfile optimized for caching by installing dependencies before copying application code. This way, if only app code changes, Docker reuses the cached dependency layers.

dockerfile
FROM python:3.11-slim

# Install dependencies first (rarely changes)
COPY requirements.txt /app/requirements.txt
RUN pip install --no-cache-dir -r /app/requirements.txt

# Copy app code (changes often)
COPY src/ /app/src/

CMD ["python", "/app/src/main.py"]
⚠️

Common Pitfalls

1. Copying all files before installing dependencies: This invalidates cache for dependency installation every time any file changes.

2. Running multiple commands separately: Each RUN creates a new layer, increasing build time and image size.

3. Changing order of instructions frequently: Causes cache misses and slower builds.

dockerfile
### Wrong way (cache invalidated often)
FROM node:18
COPY . /app
RUN npm install

### Right way (cache optimized)
FROM node:18
COPY package.json package-lock.json /app/
RUN npm install
COPY . /app/
📊

Quick Reference

  • Order Dockerfile instructions from least to most frequently changed.
  • Copy dependency files separately before app code.
  • Combine related RUN commands to reduce layers.
  • Use .dockerignore to exclude unnecessary files.
  • Avoid changing base images often to keep cache effective.

Key Takeaways

Order Dockerfile instructions so stable layers come first to maximize cache reuse.
Copy dependency files and install dependencies before copying app code.
Combine multiple commands in one RUN to reduce image layers and speed builds.
Use .dockerignore to prevent unnecessary files from invalidating cache.
Avoid frequent changes to base images and early layers to keep cache effective.