Bird
Raised Fist0
Dockerdevops~5 mins

Combining RUN commands in Docker - Commands & Configuration

Choose your learning style10 modes available
Introduction
When building Docker images, each RUN command creates a new layer. Combining multiple commands into one RUN reduces image size and speeds up builds by minimizing layers.
When you want to install multiple packages in one step to keep the image small
When you need to run several setup commands that depend on each other
When you want to clean temporary files after installation in the same step
When you want to reduce the number of layers to improve build speed
When you want to keep your Dockerfile clean and efficient
Config File - Dockerfile
Dockerfile
FROM ubuntu:22.04

RUN apt-get update && apt-get install -y \
    curl \
    git \
    && rm -rf /var/lib/apt/lists/*

CMD ["bash"]

FROM sets the base image.

RUN combines update, install, and cleanup commands using && and line continuation \ for readability.

This reduces image layers and removes temporary files to keep the image small.

CMD sets the default command to run a bash shell.

Commands
Builds the Docker image named 'combined-run-example' using the Dockerfile in the current directory. This runs the combined RUN commands in one layer.
Terminal
docker build -t combined-run-example .
Expected OutputExpected
[+] Building 5.3s (7/7) FINISHED => [internal] load build definition from Dockerfile => => transferring dockerfile: 123B => [internal] load .dockerignore => => transferring context: 2B => [internal] load metadata for docker.io/library/ubuntu:22.04 => [1/3] FROM docker.io/library/ubuntu:22.04@sha256:... => CACHED [2/3] RUN apt-get update && apt-get install -y curl git && rm -rf /var/lib/apt/lists/* => [3/3] CMD ["bash"] => exporting to image => => exporting layers => => writing image sha256:... => => naming to docker.io/library/combined-run-example
→
-t - Assigns a name and optionally a tag to the image
Lists the Docker images filtered by the name 'combined-run-example' to verify the image was built.
Terminal
docker images combined-run-example
Expected OutputExpected
REPOSITORY TAG IMAGE ID CREATED SIZE combined-run-example latest abcdef123456 10 seconds ago 88MB
Runs a container from the image and checks that curl and git are installed by printing their versions.
Terminal
docker run --rm combined-run-example bash -c "curl --version && git --version"
Expected OutputExpected
curl 7.81.0 (x86_64-pc-linux-gnu) libcurl/7.81.0 OpenSSL/3.0.2 zlib/1.2.11 Release-Date: 2022-03-30 Protocols: dict file ftp ftps gopher http https imap imaps pop3 pop3s rtsp smb smbs smtp smtps telnet tftp Features: AsynchDNS HTTPS-proxy IPv6 Largefile NTLM NTLM_WB SSL TLS-SRP UnixSockets git version 2.34.1
→
--rm - Automatically removes the container after it exits
Key Concept

If you remember nothing else from this pattern, remember: combining multiple commands in one RUN reduces image size and speeds up builds by creating fewer layers.

Common Mistakes
Writing multiple RUN commands separately instead of combining them
Each RUN creates a new layer, increasing image size and build time.
Combine commands with && in a single RUN to reduce layers and clean up temporary files in the same step.
Not cleaning up temporary files after installing packages
Temporary files remain in the image, making it larger than necessary.
Use commands like rm -rf /var/lib/apt/lists/* in the same RUN to remove temporary files.
Summary
Use one RUN command with && to combine multiple shell commands.
Cleaning up temporary files in the same RUN keeps the image small.
Fewer RUN commands mean fewer image layers and faster builds.

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

  1. Step 1: Understand Docker image layers

    Each RUN instruction creates a new image layer, increasing image size.
  2. Step 2: Combine commands to reduce layers

    Combining commands with && in one RUN reduces layers and image size.
  3. Final Answer:

    To reduce the number of image layers and keep the image size smaller -> Option C
  4. 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?
easy
A. RUN apt-get update && apt-get install -y curl
B. RUN apt-get update; apt-get install -y curl
C. RUN apt-get update | apt-get install -y curl
D. RUN apt-get update &&& apt-get install -y curl

Solution

  1. Step 1: Identify correct command chaining operator

    The && operator chains commands so the second runs only if the first succeeds.
  2. Step 2: Check each option's syntax

    RUN apt-get update && apt-get install -y curl uses && correctly; others use invalid or unsafe separators.
  3. Final Answer:

    RUN apt-get update && apt-get install -y curl -> Option A
  4. Quick Check:

    Use && to chain commands safely [OK]
Hint: Use && to chain commands safely in RUN [OK]
Common Mistakes:
  • Using semicolon without error checking
  • Using invalid operators like | or &&&
  • Not chaining commands properly causing build errors
3. What will be the result of this Dockerfile snippet?
RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/*
medium
A. Creates three separate layers for each command
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

  1. Step 1: Analyze the chained RUN commands

    The commands update packages, install curl, then remove apt cache files in one RUN.
  2. Step 2: Understand layer creation

    All commands combined in one RUN create a single image layer with all changes.
  3. Final Answer:

    Updates package lists, installs curl, and cleans temporary files in one layer -> Option D
  4. 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

  1. Step 1: Understand the & operator in shell

    Single & runs the first command in background, not waiting for completion.
  2. Step 2: Impact on Docker build

    This causes the second command to run immediately, possibly before update finishes, leading to errors.
  3. Final Answer:

    The single & runs commands in background, causing build errors -> Option B
  4. 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

  1. Step 1: Combine update and install commands safely

    Use && to chain update and install commands to ensure sequential success.
  2. 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.
  3. 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.
  4. Final Answer:

    RUN apt-get update && apt-get install -y curl git && rm -rf /var/lib/apt/lists/* -> Option A
  5. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard