Bird
Raised Fist0
Dockerdevops~10 mins

Squashing layers in Docker - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - Squashing layers
Start Dockerfile Build
↓
Create Layer 1
↓
Create Layer 2
↓
Create Layer 3
↓
Squash Layers
↓
Single Layer Image
↓
Finish Build
Docker builds images in layers for each command, squashing combines multiple layers into one to reduce image size.
Execution Sample
Docker
FROM alpine
RUN apk add --no-cache curl
RUN echo 'Hello World' > /hello.txt
Builds an image with Alpine, installs curl, and adds a hello.txt file in separate layers.
Process Table
StepDockerfile CommandLayer CreatedLayer SizeImage Size After Step
1FROM alpineBase layer5MB5MB
2RUN apk add --no-cache curlLayer 22MB7MB
3RUN echo 'Hello World' > /hello.txtLayer 31KB7.001MB
4Squash layers 2 and 3Single combined layer2.001MB7.001MB
💡 Layers 2 and 3 are combined into one to reduce image size and number of layers.
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3After Squash
Image Size0MB5MB7MB7.001MB7.001MB
Number of Layers01232
Key Moments - 3 Insights
Why does the image size not reduce after adding the last small file layer before squashing?
Because each RUN command creates a new layer that adds to the image size cumulatively, as shown in step 3 of the execution_table.
What exactly does squashing do to the layers?
Squashing merges multiple layers into one, reducing the number of layers and sometimes the total image size, as seen in step 4 of the execution_table.
Does squashing remove the base layer?
No, the base layer remains unchanged; squashing only combines the subsequent layers, as shown in the concept_flow and execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the image size after step 2?
A7MB
B5MB
C2MB
D7.001MB
💡 Hint
Check the 'Image Size After Step' column for step 2 in the execution_table.
At which step does the number of layers reduce due to squashing?
AStep 3
BStep 1
CStep 4
DStep 2
💡 Hint
Look at the 'Number of Layers' row in variable_tracker after each step.
If the last RUN command added a 5MB file instead of 1KB, how would the image size after step 3 change?
AIt would stay 7.001MB
BIt would be about 12MB
CIt would be 5MB
DIt would be 2MB
💡 Hint
Add the new file size to the image size after step 2 from variable_tracker.
Concept Snapshot
Docker images build in layers, each RUN or command adds a layer.
Squashing merges multiple layers into one to reduce image size and layer count.
Base layer stays intact; only subsequent layers are squashed.
Use squashing to optimize image size and speed up deployment.
Full Transcript
Docker builds images by creating layers for each command in the Dockerfile. Each RUN command adds a new layer, increasing the image size. Squashing layers means combining multiple layers into a single one to reduce the total image size and number of layers. This helps make images smaller and faster to deploy. The base layer from the FROM command remains unchanged. In the example, installing curl and adding a file create separate layers, which are then squashed into one combined layer. This process reduces overhead and optimizes the image.

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

  1. Step 1: Understand what layers are in Docker images

    Docker images are made of layers created by each command in the Dockerfile.
  2. Step 2: Purpose of squashing layers

    Squashing merges these layers into one to reduce the final image size and improve efficiency.
  3. Final Answer:

    To combine multiple image layers into a single layer to reduce image size -> Option C
  4. 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

  1. Step 1: Check the correct flag format for squashing

    The correct flag is --squash without an equals sign or single dash.
  2. Step 2: Validate the full command syntax

    The command docker build --squash -t myimage . correctly uses the flag and tags the image.
  3. Final Answer:

    docker build --squash -t myimage . -> Option D
  4. 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

  1. Step 1: Understand how layers affect image size

    Each RUN command creates a new layer, adding to the total image size.
  2. Step 2: Effect of --squash on layers

    Using --squash merges layers, removing intermediate data and reducing size.
  3. Final Answer:

    The image with --squash is smaller because layers are combined -> Option A
  4. Quick Check:

    Squash reduces size by merging layers [OK]
Hint: Squash merges layers, reducing image size [OK]
Common Mistakes:
  • Thinking squashing adds metadata and increases size
  • Believing layers do not affect image size
  • Confusing caching with squashing effects
4. You run docker build --squash -t myapp . but get an error saying the flag is unknown. What is the most likely cause?
medium
A. The image tag is missing
B. Docker experimental features are not enabled
C. The Dockerfile has syntax errors
D. The build context path is incorrect

Solution

  1. Step 1: Understand requirements for --squash flag

    The --squash flag requires Docker experimental features to be enabled.
  2. Step 2: Identify cause of unknown flag error

    If experimental features are off, Docker does not recognize --squash and throws an error.
  3. Final Answer:

    Docker experimental features are not enabled -> Option B
  4. Quick Check:

    Enable experimental features to use --squash [OK]
Hint: Enable experimental features to use --squash [OK]
Common Mistakes:
  • Assuming Dockerfile errors cause unknown flag
  • Thinking missing tag causes flag error
  • Blaming build context path for flag issues
5. You want to reduce your Docker image size by squashing layers but also keep some intermediate layers for caching. Which approach is best?
hard
A. Use multi-stage builds and squash only the final stage
B. Use --squash on every build to squash all layers
C. Avoid squashing and rely on layer caching only
D. Manually delete intermediate files after build

Solution

  1. Step 1: Understand multi-stage builds

    Multi-stage builds allow creating intermediate images for caching and a final optimized image.
  2. Step 2: Combine multi-stage builds with squashing

    Squashing only the final stage reduces image size while keeping intermediate layers for cache.
  3. Final Answer:

    Use multi-stage builds and squash only the final stage -> Option A
  4. Quick Check:

    Multi-stage + squash final stage = best size and cache [OK]
Hint: Squash final stage in multi-stage builds for best results [OK]
Common Mistakes:
  • Squashing all layers loses caching benefits
  • Avoiding squashing misses size reduction
  • Deleting files manually is error-prone
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard