0
0
Dockerdevops~5 mins

Layer caching and ordering in Docker - Commands & Configuration

Choose your learning style9 modes available
Introduction
Building Docker images can be slow if every step runs from scratch. Layer caching helps speed this up by reusing unchanged steps. Ordering commands well in the Dockerfile makes caching more effective and builds faster.
When you want to speed up Docker image builds by reusing unchanged steps.
When you update your application code often but dependencies rarely change.
When you want to reduce build time during development cycles.
When you want to keep your Docker images small and efficient.
When you want to avoid downloading the same packages repeatedly.
Config File - Dockerfile
Dockerfile
FROM python:3.11-slim

# Install dependencies first to leverage caching
COPY requirements.txt /app/requirements.txt
RUN pip install --no-cache-dir -r /app/requirements.txt

# Copy application code
COPY app/ /app/

WORKDIR /app

CMD ["python", "app.py"]

FROM: sets the base image.

COPY requirements.txt: copies dependency list first to cache this step if unchanged.

RUN pip install: installs dependencies, cached if requirements.txt unchanged.

COPY app/: copies app code, triggers rebuild only if code changes.

WORKDIR: sets working directory.

CMD: runs the app.

Commands
Builds the Docker image named 'my-app' with tag '1.0' using the Dockerfile in the current directory. This creates layers and caches them for future builds.
Terminal
docker build -t my-app:1.0 .
Expected OutputExpected
Sending build context to Docker daemon 12.3MB Step 1/6 : FROM python:3.11-slim ---> 3a2f1c4b5d6e Step 2/6 : COPY requirements.txt /app/requirements.txt ---> Using cache ---> 7b8c9d0e1f2a Step 3/6 : RUN pip install --no-cache-dir -r /app/requirements.txt ---> Running in 4d5e6f7a8b9c Collecting flask Installing collected packages: flask Successfully installed flask-2.3.2 Removing intermediate container 4d5e6f7a8b9c ---> 1a2b3c4d5e6f Step 4/6 : COPY app/ /app/ ---> 9f8e7d6c5b4a Step 5/6 : WORKDIR /app ---> Running in 123abc456def Removing intermediate container 123abc456def ---> 0f1e2d3c4b5a Step 6/6 : CMD ["python", "app.py"] ---> Running in abc123def456 Removing intermediate container abc123def456 ---> 5a6b7c8d9e0f Successfully built 5a6b7c8d9e0f Successfully tagged my-app:1.0
-t - Assigns a name and tag to the image for easy reference.
Rebuilds the image after changing only the app code. Docker reuses cached layers for dependencies, speeding up the build.
Terminal
docker build -t my-app:1.1 .
Expected OutputExpected
Sending build context to Docker daemon 12.5MB Step 1/6 : FROM python:3.11-slim ---> 3a2f1c4b5d6e Step 2/6 : COPY requirements.txt /app/requirements.txt ---> Using cache ---> 7b8c9d0e1f2a Step 3/6 : RUN pip install --no-cache-dir -r /app/requirements.txt ---> Using cache ---> 1a2b3c4d5e6f Step 4/6 : COPY app/ /app/ ---> Running in 9f8e7d6c5b4a Removing intermediate container 9f8e7d6c5b4a ---> 2a3b4c5d6e7f Step 5/6 : WORKDIR /app ---> Running in 789abc123def Removing intermediate container 789abc123def ---> 0f1e2d3c4b5a Step 6/6 : CMD ["python", "app.py"] ---> Running in def456abc123 Removing intermediate container def456abc123 ---> 6b7c8d9e0f1a Successfully built 6b7c8d9e0f1a Successfully tagged my-app:1.1
-t - Assigns a new tag to the rebuilt image.
Shows the layers of the image and which steps were cached or rebuilt, helping understand caching effectiveness.
Terminal
docker history my-app:1.1
Expected OutputExpected
IMAGE CREATED CREATED BY SIZE COMMENT 6b7c8d9e0f1a 2 minutes ago CMD ["python", "app.py"] 0B 0f1e2d3c4b5a 2 minutes ago WORKDIR /app 0B 2a3b4c5d6e7f 2 minutes ago COPY app/ /app/ 1.2MB 1a2b3c4d5e6f 10 minutes ago RUN pip install --no-cache-dir -r /app/requirements.txt 50MB 7b8c9d0e1f2a 10 minutes ago COPY requirements.txt /app/requirements.txt 1kB 3a2f1c4b5d6e 1 day ago FROM python:3.11-slim 120MB
Key Concept

If you order your Dockerfile so that rarely changing steps come first, Docker can reuse cached layers and speed up builds.

Common Mistakes
Copying application code before installing dependencies in the Dockerfile.
This causes Docker to rerun dependency installation every time app code changes, slowing builds.
Copy and install dependencies first, then copy app code to maximize cache reuse.
Changing the requirements.txt file frequently without need.
This invalidates the cache for dependency installation, causing slow rebuilds.
Only update requirements.txt when dependencies actually change.
Summary
Docker builds images in layers and caches each step to speed up future builds.
Ordering Dockerfile commands so dependencies install before app code helps reuse cache.
Use 'docker build' to create images and 'docker history' to inspect layer caching.