Lesson 22: Optimizing Image Layers and Caching
Efficient Docker image building relies heavily on understanding how Docker caches layers. If an instruction in the Dockerfile hasn't changed, Docker reuses the existing layer, speeding up the build process.
1. Ordering Instructions Correctly
Docker evaluates layers from top to bottom. If a layer changes, all subsequent layers are invalidated and must be rebuilt.
Strategy: Place the least frequently changing instructions (like the base OS and static dependencies) at the top, and the most frequently changing instructions (like copying application code) at the bottom.
Bad Example (High Cache Busting)
dockerfile COPY . /app # Change in code invalidates everything below RUN apt-get update && apt-get install -y ... RUN npm install # Will run every time the code changes
Good Example (Optimized Caching)
dockerfile
1. Install static dependencies (rarely changes)
RUN apt-get update && apt-get install -y python3
2. Copy dependency definition files (e.g., requirements.txt) (changes rarely)
COPY requirements.txt /app/ RUN pip install -r requirements.txt # Cached if requirements.txt doesn't change
3. Copy application source code (changes frequently)
COPY . /app/
2. Reducing the Number of Layers (Chaining RUN)
As discussed in Lesson 9, chain commands in a single RUN instruction using && \ to reduce the total number of layers. Each RUN creates a layer, which adds overhead.
3. Using .dockerignore
To prevent unnecessary files (like .git history, node_modules, or local temporary files) from being sent to the Docker daemon during the build context, create a .dockerignore file.
.dockerignore content
.git .gitignore node_modules temp/
This drastically speeds up the build process, especially for large projects, by minimizing the data that must be transferred.