0
0
Dockerdevops~5 mins

Minimizing layers in Docker - Commands & Configuration

Choose your learning style9 modes available
Introduction
Docker images are made of layers created by each command in a Dockerfile. Minimizing layers helps keep images smaller and faster to download and run.
When you want your Docker images to use less disk space on your computer or server
When you need to speed up the time it takes to build and push images to a registry
When you want to reduce the time it takes to pull images on other machines
When you want to keep your Docker images clean and easier to manage
When you want to avoid unnecessary layers that increase image size without adding value
Config File - Dockerfile
Dockerfile
FROM python:3.11-slim

# Combine update and install in one RUN to minimize layers
RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/*

# Set working directory
WORKDIR /app

# Copy application code
COPY app.py /app/app.py

# Run the application
CMD ["python", "app.py"]

FROM: sets the base image.

RUN: combines update and install commands with cleanup in one layer to reduce image size.

WORKDIR: sets the working directory inside the container.

COPY: adds application code.

CMD: defines the command to run the app.

Commands
Builds the Docker image named 'my-app' with tag '1.0' using the Dockerfile in the current directory. This creates the image with minimized layers as defined.
Terminal
docker build -t my-app:1.0 .
Expected OutputExpected
Sending build context to Docker daemon 3.07kB Step 1/6 : FROM python:3.11-slim ---> 3a1b2c3d4e5f Step 2/6 : RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/* ---> Using cache ---> 7f8e9d0a1b2c Step 3/6 : WORKDIR /app ---> Using cache ---> 1a2b3c4d5e6f Step 4/6 : COPY app.py /app/app.py ---> Using cache ---> 9a8b7c6d5e4f Step 5/6 : CMD ["python", "app.py"] ---> Using cache ---> 0f1e2d3c4b5a Successfully built 0f1e2d3c4b5a Successfully tagged my-app:1.0
Shows the details of the built image to verify its size and tag.
Terminal
docker images my-app:1.0
Expected OutputExpected
REPOSITORY TAG IMAGE ID CREATED SIZE my-app 1.0 0f1e2d3c4b5a 2 minutes ago 120MB
Displays the layers of the image to confirm that the number of layers is minimized.
Terminal
docker history my-app:1.0
Expected OutputExpected
IMAGE CREATED CREATED BY SIZE COMMENT 0f1e2d3c4b5a 2 minutes ago CMD ["python" "app.py"] 0B 1a2b3c4d5e6f 2 minutes ago WORKDIR /app 0B 9a8b7c6d5e4f 2 minutes ago COPY app.py /app/app.py 1.2kB 7f8e9d0a1b2c 2 minutes ago RUN apt-get update && apt-get install -y curl… 45MB 3a1b2c3d4e5f 3 days ago FROM python:3.11-slim 75MB
Key Concept

Each command in a Dockerfile creates a layer, so combining commands reduces the total layers and image size.

Common Mistakes
Using multiple RUN commands for related tasks like update and install separately
Each RUN creates a new layer, increasing image size unnecessarily
Combine related commands with && in a single RUN to create one layer
Not cleaning up temporary files after package installation
Temporary files remain in the layer, making the image larger
Remove cache or temporary files in the same RUN command after installation
Summary
Docker images are built in layers, one per command in the Dockerfile.
Combining commands like update and install in one RUN reduces layers and image size.
Cleaning up temporary files in the same RUN command keeps images smaller and cleaner.