0
0
Dockerdevops~5 mins

Image layers concept in Docker - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you build a Docker image, it is made up of layers. Each layer represents a step in the build process and stores changes like adding files or installing software. This helps Docker reuse layers to save time and space when building or running containers.
When you want to speed up building Docker images by reusing unchanged parts.
When you want to understand why your Docker image is large and how to make it smaller.
When you want to update only part of your application without rebuilding everything.
When you want to share common parts of images between different projects to save disk space.
When you want to debug which step in your Dockerfile caused an error or added unwanted files.
Config File - Dockerfile
Dockerfile
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
COPY . ./
CMD ["python", "app.py"]

This Dockerfile has multiple layers:

  • FROM python:3.11-slim sets the base image layer.
  • WORKDIR /app creates a new layer setting the working directory.
  • COPY requirements.txt ./ adds the requirements file as a layer.
  • RUN pip install ... installs dependencies in a separate layer.
  • COPY . ./ copies the app code as another layer.
  • CMD [...] sets the command to run the app, creating metadata but not a filesystem layer.

Each step creates a layer that Docker caches and reuses if unchanged.

Commands
Builds the Docker image from the Dockerfile in the current folder. Docker creates layers for each step and caches them for reuse.
Terminal
docker build -t my-app-image .
Expected OutputExpected
Sending build context to Docker daemon 5.12kB Step 1/6 : FROM python:3.11-slim ---> 3a1b2c4d5e6f Step 2/6 : WORKDIR /app ---> Using cache ---> 7f8e9d0a1b2c Step 3/6 : COPY requirements.txt ./ ---> Using cache ---> 4d5e6f7a8b9c Step 4/6 : RUN pip install --no-cache-dir -r requirements.txt ---> Running in 123abc456def Collecting flask Installing collected packages: flask Successfully installed flask-2.2.2 Removing intermediate container 123abc456def ---> 9a0b1c2d3e4f Step 5/6 : COPY . ./ ---> 5f6e7d8c9b0a Step 6/6 : CMD ["python", "app.py"] ---> Running in 789abc012def Removing intermediate container 789abc012def ---> 1a2b3c4d5e6f Successfully built 1a2b3c4d5e6f Successfully tagged my-app-image:latest
-t - Assigns a name and optionally a tag to the image
Shows the layers of the built image with their size and commands that created them.
Terminal
docker history my-app-image
Expected OutputExpected
IMAGE CREATED CREATED BY SIZE COMMENT 1a2b3c4d5e6f 2 minutes ago CMD ["python", "app.py"] 0B 5f6e7d8c9b0a 2 minutes ago COPY . ./ 1.2MB 9a0b1c2d3e4f 2 minutes ago RUN pip install --no-cache-dir -r requirements.txt 15MB 4d5e6f7a8b9c 2 minutes ago COPY requirements.txt ./ 2kB 7f8e9d0a1b2c 2 minutes ago WORKDIR /app 0B 3a1b2c4d5e6f 3 days ago FROM python:3.11-slim 45MB
Runs a container from the image to verify it works and prints the Python version installed in the image.
Terminal
docker run --rm my-app-image python --version
Expected OutputExpected
Python 3.11.4
--rm - Automatically removes the container after it exits
Key Concept

Docker images are built in layers, each representing a step that can be cached and reused to speed up builds and save space.

Common Mistakes
Changing a file early in the Dockerfile causes all following layers to rebuild.
Docker caches layers in order; if an early layer changes, all later layers are rebuilt, slowing builds.
Order Dockerfile steps to put frequently changing files last, so earlier layers stay cached.
Copying the entire app before installing dependencies.
If app code changes, dependency installation layer is rebuilt unnecessarily.
Copy only dependency files first, install dependencies, then copy app code.
Not using .dockerignore to exclude unnecessary files.
Unneeded files increase build context size and image layers, making builds slower and images larger.
Create a .dockerignore file to exclude files like .git, node_modules, or temp files.
Summary
Docker images are made of layers created by each step in the Dockerfile.
Docker caches layers to reuse unchanged parts and speed up builds.
Ordering Dockerfile steps well helps keep builds fast and images small.