What is a common way to reduce Docker image size by choosing the base image?
Use a minimal base image like Alpine Linux instead of a full OS image. Alpine is very small and includes only essential packages.
Click to reveal answer
beginner
How does combining RUN commands in a Dockerfile help reduce image size?
Combining multiple RUN commands into one reduces the number of layers, which lowers the final image size.
Click to reveal answer
intermediate
Why should you remove package manager caches and temporary files in Docker images?
Removing caches and temp files after installing packages prevents unnecessary data from staying in the image, making it smaller.
Click to reveal answer
intermediate
What is a multi-stage build in Docker and how does it help reduce image size?
Multi-stage builds use multiple FROM statements to build in stages. You copy only the final needed artifacts to the last stage, leaving behind build tools and files, reducing image size.
Click to reveal answer
beginner
How can using .dockerignore file help in reducing Docker image size?
The .dockerignore file excludes unnecessary files and folders from the build context, so they are not copied into the image, keeping it smaller.
Click to reveal answer
Which base image is known for being very small and commonly used to reduce Docker image size?
ACentOS
BUbuntu
CAlpine
DDebian
✗ Incorrect
Alpine is a minimal Linux distribution designed to be small and secure, making it popular for small Docker images.
What is the benefit of combining multiple RUN commands into a single RUN command in a Dockerfile?
AReduces the number of image layers
BSpeeds up container startup time
CIncreases image caching
DImproves network security
✗ Incorrect
Combining RUN commands reduces the number of layers, which helps reduce the final image size.
What does a multi-stage Docker build allow you to do?
ACompress images after build
BRun multiple containers from one image
CAutomatically update base images
DBuild images in stages and copy only needed files to the final image
✗ Incorrect
Multi-stage builds let you separate build and runtime environments, copying only necessary files to keep the final image small.
Why should you clean up package manager caches in a Dockerfile?
ATo speed up package installation
BTo reduce image size by removing unnecessary files
CTo improve container security
DTo enable automatic updates
✗ Incorrect
Package manager caches are temporary files that increase image size if not removed.
What is the purpose of the .dockerignore file?
AExclude files from the build context to reduce image size
BSpecify environment variables for the container
CDefine network settings for the container
DList commands to run after container start
✗ Incorrect
The .dockerignore file tells Docker which files to ignore when sending the build context, helping reduce image size.
Explain three strategies to reduce Docker image size and why they work.
Think about base image choice, Dockerfile commands, and cleanup steps.
You got /3 concepts.
Describe how multi-stage builds help in creating smaller Docker images.
Focus on separating build environment from final image.
You got /4 concepts.
Practice
(1/5)
1. Which of the following is the best initial step to reduce the size of a Docker image?
easy
A. Start with a small base image like Alpine or slim variants
B. Install all available packages to avoid missing dependencies
C. Use the latest Ubuntu image without modifications
D. Add unnecessary files to the image for future use
Solution
Step 1: Understand base image impact
The base image sets the starting size; smaller bases mean smaller final images.
Step 2: Compare options
Alpine and slim variants are minimal, unlike full Ubuntu or adding unnecessary files.
Final Answer:
Start with a small base image like Alpine or slim variants -> Option A
Quick Check:
Small base image = smaller Docker image [OK]
Hint: Choose minimal base images first to reduce size [OK]
Common Mistakes:
Using full OS images without slimming
Installing unnecessary packages
Adding extra files without need
2. Which Dockerfile command sequence correctly cleans up package manager caches to reduce image size?
easy
A. RUN apt-get update && rm -rf /var/lib/apt/lists/* && apt-get install curl
B. RUN apt-get install curl && apt-get update
C. RUN rm -rf /var/lib/apt/lists/* && apt-get install curl
D. RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/*
Solution
Step 1: Understand package installation order
First update package lists, then install packages, finally clean cache to reduce image size.
Step 2: Analyze commands
RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/* updates, installs, then removes cache correctly; others have wrong order causing errors or no cleanup.
Final Answer:
RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/* -> Option D
Quick Check:
Update -> install -> clean cache = correct order [OK]
Hint: Update before install, clean cache after install [OK]
Common Mistakes:
Cleaning cache before installing packages
Skipping apt-get update
Wrong command order causing errors
3. Given this Dockerfile snippet, what will be the approximate size effect on the final image?
FROM node:18-alpine
RUN apk add --no-cache git
COPY . /app
RUN npm install
medium
A. The image will be small because Alpine and --no-cache reduce size
B. The image will be large because npm install always adds big size
C. The image will be large because git is installed without cache cleanup
D. The image size is unaffected by base image or package installs
Solution
Step 1: Analyze base image and package install
Using Alpine base and apk with --no-cache avoids cache files, keeping image small.
Step 2: Consider npm install impact
npm install adds dependencies but starting from Alpine keeps overall size smaller than heavier bases.
Final Answer:
The image will be small because Alpine and --no-cache reduce size -> Option A
Quick Check:
Alpine + --no-cache = smaller image [OK]
Hint: Use Alpine + --no-cache to keep image small [OK]
Common Mistakes:
Assuming npm install always makes image large
Ignoring --no-cache effect
Not considering base image size
4. You wrote this Dockerfile to reduce image size but the final image is still large:
FROM python:3.12
RUN pip install flask
RUN rm -rf /root/.cache/pip
What is the main issue causing the large image?
medium
A. The pip cache is not cleaned because it is stored elsewhere
B. You should combine RUN commands to reduce layers
C. The base image python:3.12 is already large and cannot be reduced
D. Flask installation always creates large images regardless
Solution
Step 1: Understand Docker layers
Each RUN creates a new layer; cleaning cache in a separate RUN keeps cache in previous layer.
Step 2: Combine commands to remove cache in same layer
Combining pip install and cache removal in one RUN prevents cache from persisting in earlier layers.
Final Answer:
You should combine RUN commands to reduce layers -> Option B
Quick Check:
Combine RUN commands to clean caches in one layer [OK]
Hint: Combine install and cleanup in one RUN to reduce layers [OK]
Common Mistakes:
Cleaning cache in separate RUN commands
Assuming base image size can't be optimized
Believing package size can't be reduced
5. You want to build a Docker image for a Go application but keep it as small as possible. Which multi-stage Dockerfile snippet achieves this best?
FROM golang:1.20 AS builder
WORKDIR /app
COPY . .
RUN go build -o myapp
FROM alpine:latest
COPY --from=builder /app/myapp /usr/local/bin/myapp
CMD ["myapp"]
hard
A. This build copies all source files to Alpine, increasing image size unnecessarily
B. Using Alpine as final image is bad because it lacks Go runtime
C. This multi-stage build compiles in golang image and copies only binary to Alpine, minimizing size
D. You should install Go in Alpine to build and run in one stage
Solution
Step 1: Understand multi-stage build purpose
Build stage uses full Go image; final stage uses minimal Alpine with only binary copied.
Step 2: Analyze final image contents
Only the compiled binary is copied, no source or build tools, keeping image small.
Final Answer:
This multi-stage build compiles in golang image and copies only binary to Alpine, minimizing size -> Option C
Quick Check:
Multi-stage build + minimal final image = small size [OK]
Hint: Use multi-stage builds to copy only final artifacts [OK]
Common Mistakes:
Copying source files to final image
Installing build tools in final image
Building and running in same large image
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.