📖 Scenario: You are creating a Docker image for a simple web application. To keep the image small and efficient, you want to combine multiple RUN commands into one.
🎯 Goal: Build a Dockerfile that installs curl and vim using a single RUN command.
📋 What You'll Learn
Create a Dockerfile starting from the ubuntu:20.04 base image
Use a single RUN command to update package lists and install curl and vim
Use apt-get update and apt-get install -y curl vim combined with &&
End the RUN command with rm -rf /var/lib/apt/lists/* to clean up
Print the Dockerfile content at the end
💡 Why This Matters
🌍 Real World
Combining RUN commands in Dockerfiles helps create smaller, faster, and cleaner Docker images used in real-world software deployment.
💼 Career
Understanding how to optimize Dockerfiles is a key skill for DevOps engineers and developers working with containerized applications.
Progress0 / 4 steps
1
Create the base Dockerfile
Create a Dockerfile starting with the line FROM ubuntu:20.04.
Docker
Hint
The first line in a Dockerfile specifies the base image using FROM.
2
Add a combined RUN command
Add a single RUN command that updates the package list with apt-get update and installs curl and vim using apt-get install -y curl vim. Combine these commands using &&.
Docker
Hint
Use RUN apt-get update && apt-get install -y curl vim to combine the commands.
3
Add cleanup to the RUN command
Extend the existing RUN command to also remove the package lists by adding rm -rf /var/lib/apt/lists/* at the end, combined with &&.
Docker
Hint
Use && rm -rf /var/lib/apt/lists/* to clean up after installing.
4
Print the Dockerfile content
Write a command to print the content of the Dockerfile using cat Dockerfile.
Docker
Hint
Use cat Dockerfile to display the file content.
Practice
(1/5)
1. Why is it recommended to combine multiple commands in a single RUN instruction in a Dockerfile?
easy
A. To make the Dockerfile longer and more complex
B. To run commands in parallel for faster builds
C. To reduce the number of image layers and keep the image size smaller
D. To avoid using the shell in Dockerfile commands
Solution
Step 1: Understand Docker image layers
Each RUN instruction creates a new image layer, increasing image size.
Step 2: Combine commands to reduce layers
Combining commands with && in one RUN reduces layers and image size.
Final Answer:
To reduce the number of image layers and keep the image size smaller -> Option C
Quick Check:
Combining RUN commands = fewer layers [OK]
Hint: Combine commands with && to reduce image layers [OK]
Common Mistakes:
Thinking combining commands makes builds faster
Believing each command runs in parallel
Ignoring image size impact of multiple RUNs
2. Which of the following is the correct syntax to combine two commands in a single RUN instruction in a Dockerfile?
B. Only updates package lists and installs curl, but does not clean files
C. Fails because rm command is invalid in RUN
D. Updates package lists, installs curl, and cleans temporary files in one layer
Solution
Step 1: Analyze the chained RUN commands
The commands update packages, install curl, then remove apt cache files in one RUN.
Step 2: Understand layer creation
All commands combined in one RUN create a single image layer with all changes.
Final Answer:
Updates package lists, installs curl, and cleans temporary files in one layer -> Option D
Quick Check:
Combined RUN commands = single layer with all steps [OK]
Hint: One RUN with && creates one layer with all commands [OK]
Common Mistakes:
Thinking each command creates a separate layer
Assuming rm command is invalid in RUN
Ignoring cleaning temporary files effect
4. You wrote this Dockerfile line:
RUN apt-get update & apt-get install -y git
What is the problem with this command?
medium
A. The & operator is correct and recommended for RUN commands
B. The single & runs commands in background, causing build errors
C. The commands will run sequentially without errors
D. The command will install git twice
Solution
Step 1: Understand the & operator in shell
Single & runs the first command in background, not waiting for completion.
Step 2: Impact on Docker build
This causes the second command to run immediately, possibly before update finishes, leading to errors.
Final Answer:
The single & runs commands in background, causing build errors -> Option B
Quick Check:
Use && not & to chain RUN commands [OK]
Hint: Use &&, not &, to chain commands in RUN [OK]
Common Mistakes:
Using & instead of && for chaining
Assuming commands run sequentially with &
Ignoring build errors caused by background commands
5. You want to install curl and git in one RUN command and clean up apt cache to keep the image small. Which Dockerfile line correctly does this?
hard
A. RUN apt-get update && apt-get install -y curl git && rm -rf /var/lib/apt/lists/*
B. RUN apt-get update; apt-get install -y curl git; rm -rf /var/lib/apt/lists/*
C. RUN apt-get update && apt-get install -y curl && apt-get install -y git && rm -rf /var/lib/apt/lists/*
D. RUN apt-get update & apt-get install -y curl git && rm -rf /var/lib/apt/lists/*
Solution
Step 1: Combine update and install commands safely
Use && to chain update and install commands to ensure sequential success.
Step 2: Clean apt cache in the same RUN
Remove temporary files with rm -rf /var/lib/apt/lists/* in the same RUN to keep image small.
Step 3: Check options for correctness
RUN apt-get update && apt-get install -y curl git && rm -rf /var/lib/apt/lists/* chains all commands correctly in one RUN; others use semicolons or background & causing issues or inefficiencies.
Final Answer:
RUN apt-get update && apt-get install -y curl git && rm -rf /var/lib/apt/lists/* -> Option A
Quick Check:
Chain with && and clean in one RUN [OK]
Hint: Chain update, install, and cleanup with && in one RUN [OK]
Common Mistakes:
Using semicolons instead of && without error checking
Running install commands separately (inefficient)
Using & causing background execution errors
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.