0
0
Dockerdevops~10 mins

Image size and minimal base images in Docker - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Image size and minimal base images
Choose base image
Check image size
Use minimal base image?
NoUse standard base image
Yes
Add only needed packages
Build final image
Check final image size
Deploy or optimize further
This flow shows choosing a base image, deciding if a minimal image is suitable, building with only needed packages, and checking the final image size.
Execution Sample
Docker
FROM alpine:3.18
RUN apk add --no-cache curl
CMD ["curl", "--version"]
This Dockerfile uses a minimal Alpine base image, adds curl without cache to keep size small, and runs curl version command.
Process Table
StepActionImage Size (MB)Description
1Start with alpine:3.18 base image5Alpine is a minimal Linux image (~5MB)
2Run apk add --no-cache curl7Adds curl package without cache, small size increase
3Set CMD to run curl --version7No size change, sets default command
4Build final image7Final image size is about 7MB, very small
5Compare with ubuntu base image29Ubuntu base image is much larger (~29MB)
6Exit-Finished building minimal image
💡 Build finished with minimal base image resulting in a small final image size
Status Tracker
VariableStartAfter Step 2After Step 3Final
Image Size (MB)5 (alpine base)7 (after curl install)7 (after CMD set)7 (final image)
Key Moments - 3 Insights
Why does using alpine base image reduce image size compared to ubuntu?
Alpine is designed to be minimal and small (~5MB), while Ubuntu includes many packages by default making it larger (~29MB), as shown in execution_table rows 1 and 5.
What does --no-cache do in apk add command?
It prevents caching package index files, so they are not saved in the image, keeping the image size smaller, as seen in step 2 where size only increases slightly.
Does setting CMD affect image size?
No, setting CMD only defines the default command to run and does not change the image size, as shown in step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the image size after adding curl to alpine?
A5 MB
B7 MB
C29 MB
D12 MB
💡 Hint
Check the 'Image Size (MB)' column at step 2 in the execution_table
At which step does the image size increase from the base image size?
AStep 2
BStep 3
CStep 1
DStep 5
💡 Hint
Look for the first step where 'Image Size (MB)' changes from 5 to 7 in execution_table
If we used ubuntu base image instead of alpine, what would be the approximate base image size?
A5 MB
B7 MB
C29 MB
D50 MB
💡 Hint
Refer to step 5 in execution_table comparing ubuntu base image size
Concept Snapshot
Use minimal base images like alpine (~5MB) to keep Docker images small.
Add only needed packages with no cache to avoid size bloat.
Standard images like ubuntu are larger (~29MB).
Setting CMD does not affect image size.
Check image size after build to confirm minimal footprint.
Full Transcript
This lesson shows how choosing a minimal base image like alpine reduces Docker image size compared to standard images like ubuntu. We start with alpine base image of about 5MB. Adding curl with apk add --no-cache increases size slightly to about 7MB. Setting CMD does not change size. The final image remains small and efficient. Using no-cache prevents extra files from being saved, keeping the image lean. Comparing with ubuntu base image shows it is much larger at about 29MB. This teaches how minimal base images and careful package installation help keep Docker images small and fast to deploy.