📖 Scenario: You are working on a web application that needs to be deployed using Docker containers. The application image is currently very large, which causes slow downloads and uses a lot of storage space on servers.Optimizing Docker images can make your deployments faster and save resources.
🎯 Goal: Learn how to create a simple Dockerfile and then optimize the Docker image size by using a smaller base image and cleaning up unnecessary files.
📋 What You'll Learn
Create a Dockerfile with a base image and a simple command
Add a variable to specify the base image version
Modify the Dockerfile to use a smaller base image and remove unnecessary files
Build and display the size of the Docker image
💡 Why This Matters
🌍 Real World
Optimizing Docker images helps reduce download times and storage use when deploying applications in real environments.
💼 Career
Knowing how to optimize container images is a key skill for DevOps engineers to improve deployment efficiency and resource management.
Progress0 / 4 steps
1
Create a basic Dockerfile
Create a file named Dockerfile with the following content exactly: FROM python:3.12-slim CMD ["python3", "--version"]
Docker
Hint
Use the FROM instruction to specify the base image and CMD to run the command.
2
Add a base image version variable
Add a build argument named PYTHON_VERSION with value 3.12-slim and use it in the FROM line like this: ARG PYTHON_VERSION=3.12-slim FROM python:${PYTHON_VERSION}
Docker
Hint
Use ARG to define a variable and then use ${VAR_NAME} syntax in FROM.
3
Optimize the Dockerfile
Change the base image to python:3.12-alpine by setting PYTHON_VERSION=3.12-alpine and add a RUN command to remove cache files: RUN rm -rf /var/cache/apk/*
Docker
Hint
Use the Alpine base image for smaller size and clean cache files to reduce image size.
4
Build and show the Docker image size
Build the Docker image with tag optimized-python and then run docker images optimized-python --format "{{.Repository}}: {{.Size}}" to display the image size.
Docker
Hint
Use docker build -t optimized-python . to build and docker images to check size.
Practice
(1/5)
1. Why is it important to keep Docker images small and optimized?
easy
A. Larger images are always more secure
B. Optimized images require more bandwidth
C. Image size does not affect deployment speed
D. Smaller images start faster and use less disk space
Solution
Step 1: Understand image size impact on startup
Smaller images load faster because there is less data to transfer and unpack.
Step 2: Understand disk space usage
Smaller images use less disk space on the host, saving resources.
Final Answer:
Smaller images start faster and use less disk space -> Option D
Quick Check:
Small image = faster start + less space [OK]
Hint: Smaller images = faster start and less space used [OK]
Common Mistakes:
Thinking bigger images are faster
Ignoring disk space impact
Assuming image size doesn't affect deployment
2. Which Dockerfile instruction helps reduce image size by removing unnecessary files?
easy
A. RUN apt-get update
B. RUN rm -rf /var/lib/apt/lists/*
C. COPY . /app
D. EXPOSE 80
Solution
Step 1: Identify cleanup commands
Removing package lists after installation reduces image size.
Step 2: Understand the command effect
RUN rm -rf /var/lib/apt/lists/* deletes cached package files, freeing space.
Final Answer:
RUN rm -rf /var/lib/apt/lists/* -> Option B
Quick Check:
Cleanup commands reduce image size [OK]
Hint: Remove cache files after install to shrink image [OK]
Common Mistakes:
Using COPY to reduce size (it adds files)
Exposing ports does not affect size
Forgetting to clean package caches
3. Given this Dockerfile snippet:
FROM python:3.12-slim
RUN pip install flask
COPY . /app
RUN rm -rf /tmp/*
What is the main benefit of using the python:3.12-slim base image here?
medium
A. It is a smaller image, reducing build time and size
B. It disables network access for security
C. It automatically optimizes Python code
D. It includes all development tools by default
Solution
Step 1: Understand base image types
"slim" images are stripped down versions with fewer packages.
Step 2: Identify benefits of slim images
Smaller base images reduce overall image size and speed up builds.
Final Answer:
It is a smaller image, reducing build time and size -> Option A
Quick Check:
Using slim base = smaller image [OK]
Hint: Slim base images reduce size and speed builds [OK]
Common Mistakes:
Assuming slim includes all tools
Thinking slim optimizes code automatically
Confusing security features with image size
4. You have this Dockerfile snippet:
FROM node:20
RUN npm install
RUN rm -rf node_modules
COPY . /app
What is wrong with this Dockerfile regarding image optimization?
medium
A. The base image node:20 is too small
B. COPY should come before RUN commands always
C. Removing node_modules after install wastes build time and space
D. There is no EXPOSE instruction
Solution
Step 1: Analyze the RUN commands order
Installing then deleting node_modules wastes time and space.
Step 2: Understand image layers
Each RUN creates a layer; deleting files in a later layer does not reduce earlier layer size.
Final Answer:
Removing node_modules after install wastes build time and space -> Option C
Quick Check:
Deleting installed files wastes space [OK]
Hint: Don't delete installed files in later layers [OK]
Common Mistakes:
Thinking COPY order always matters here
Assuming base image size is the problem
Believing EXPOSE affects image size
5. You want to optimize a Docker image for a Python app. Which combination of steps best reduces image size and speeds deployment?
hard
A. Use a slim base image, combine RUN commands, and clean package caches
B. Use a full OS base image and install all development tools
C. Copy all files first, then install packages separately