0
0
Dockerdevops~10 mins

Squashing layers in Docker - Step-by-Step Execution

Choose your learning style9 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.