How to Optimize Dockerfile for Caching to Speed Up Builds
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.
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.
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.
### 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
RUNcommands to reduce layers. - Use
.dockerignoreto exclude unnecessary files. - Avoid changing base images often to keep cache effective.