📖 Scenario: You are working on a Docker project where you want to optimize the image size by combining multiple layers into one. This helps reduce the final image size and speeds up deployment.
🎯 Goal: Build a Dockerfile that creates a base image with multiple commands, then squash the layers into a single layer using Docker build options.
📋 What You'll Learn
Create a Dockerfile with multiple RUN commands
Add a label to the image
Use Docker build with the --squash option to combine layers
Display the final image size
💡 Why This Matters
🌍 Real World
Squashing layers helps reduce Docker image size, which saves storage and speeds up deployment in real projects.
💼 Career
Understanding layer squashing is important for DevOps roles to optimize container images and improve CI/CD pipelines.
Progress0 / 4 steps
1
Create a Dockerfile with base image and multiple RUN commands
Create a file named Dockerfile with the following content exactly: start from the alpine:latest image, then add two RUN commands: one to update the package list with apk update and another to install curl with apk add curl.
Docker
Hint
Use FROM alpine:latest as the base. Then add two separate RUN lines for update and install.
2
Add a label to the Dockerfile
Add a LABEL instruction to the existing Dockerfile that sets maintainer to devops@example.com.
Docker
Hint
Use LABEL maintainer="devops@example.com" exactly.
3
Build the Docker image with squashing layers
Use the Docker CLI to build the image from the current directory with the tag myapp:squashed and enable layer squashing using the --squash option.
Docker
Hint
Run docker build --squash -t myapp:squashed . in your terminal to build the image with squashed layers.
4
Display the final image size
Run the Docker command to show the size of the image tagged myapp:squashed.
Docker
Hint
Use docker images myapp:squashed to see the image size.
Practice
(1/5)
1. What is the main purpose of docker build --squash?
easy
A. To speed up the build process by skipping some steps
B. To create multiple images from one Dockerfile
C. To combine multiple image layers into a single layer to reduce image size
D. To automatically update the base image to the latest version
Solution
Step 1: Understand what layers are in Docker images
Docker images are made of layers created by each command in the Dockerfile.
Step 2: Purpose of squashing layers
Squashing merges these layers into one to reduce the final image size and improve efficiency.
Final Answer:
To combine multiple image layers into a single layer to reduce image size -> Option C
Quick Check:
Squashing = combining layers [OK]
Hint: Squash means merge layers to save space [OK]
Common Mistakes:
Thinking it speeds up build time
Confusing squashing with creating multiple images
Assuming it updates base images automatically
2. Which of the following is the correct syntax to build a Docker image with squashed layers?
easy
A. docker build -squash=true -t myimage .
B. docker build -squash -t myimage .
C. docker build --squash=true -t myimage .
D. docker build --squash -t myimage .
Solution
Step 1: Check the correct flag format for squashing
The correct flag is --squash without an equals sign or single dash.
Step 2: Validate the full command syntax
The command docker build --squash -t myimage . correctly uses the flag and tags the image.
Final Answer:
docker build --squash -t myimage . -> Option D
Quick Check:
Correct flag is double dash --squash [OK]
Hint: Use double dash --squash without equals sign [OK]
Common Mistakes:
Using single dash -squash
Adding =true after --squash
Misplacing the flag after the context dot
3. Given this Dockerfile:
FROM alpine
RUN echo 'Hello' > /file1
RUN echo 'World' > /file2
What is the main difference in the image size when built with and without --squash?
medium
A. The image with --squash is smaller because layers are combined
B. The image with --squash is larger because it adds extra metadata
C. Both images have the same size because layers do not affect size
D. The image without --squash is smaller because layers are cached
Solution
Step 1: Understand how layers affect image size
Each RUN command creates a new layer, adding to the total image size.
Step 2: Effect of --squash on layers
Using --squash merges layers, removing intermediate data and reducing size.
Final Answer:
The image with --squash is smaller because layers are combined -> Option A