Bird
Raised Fist0
Dockerdevops~10 mins

Combining RUN commands in Docker - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - Combining RUN commands
Start Dockerfile
↓
RUN command 1
↓
RUN command 2
↓
RUN command 3
↓
Single image layer created
↓
Next Dockerfile instruction
Docker executes combined RUN commands in one layer by chaining them with && or \ to reduce image layers.
Execution Sample
Docker
RUN apt-get update && apt-get install -y curl \
    && echo "Installation done"
This RUN command updates packages, installs curl, and prints a message in one layer.
Process Table
StepCommand PartExecutionResultLayer Created
1apt-get updateRuns package list updateSuccess: package lists updatedNo
2apt-get install -y curlInstalls curl packageSuccess: curl installedNo
3echo "Installation done"Prints messageOutput: Installation doneYes
💡 All commands combined in one RUN, so one image layer created after last command
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3
Image Layers0001
Packages UpdatedNoYesYesYes
Curl InstalledNoNoYesYes
Output MessageNoneNoneNoneInstallation done
Key Moments - 2 Insights
Why do we use && or \ to combine RUN commands?
Using && or \ combines commands into one RUN instruction, creating only one image layer as shown in execution_table step 3, which keeps the image smaller.
What happens if one command fails in the combined RUN?
If any command fails, the whole RUN fails and the image build stops, because commands are chained with && which stops on failure (see execution_table steps).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, how many image layers are created after all RUN commands?
AOne
BTwo
CThree
DZero
💡 Hint
Check the 'Layer Created' column in the execution_table rows.
At which step is curl installed according to the execution table?
AStep 1
BStep 3
CStep 2
DNone
💡 Hint
Look at the 'Command Part' and 'Result' columns for curl installation.
If the first command fails, what happens to the image build?
AIt continues to next commands
BIt stops immediately
CIt skips the failed command
DIt creates a partial layer
💡 Hint
Recall that commands are chained with && which stops on failure, as explained in key_moments.
Concept Snapshot
RUN commands in Dockerfile can be combined using && or \ to run multiple commands in one layer.
This reduces image size by creating fewer layers.
If any command fails, the whole RUN fails and build stops.
Use backslash \ for line continuation for readability.
Example: RUN cmd1 && cmd2 && cmd3
Full Transcript
In Dockerfiles, combining RUN commands means chaining multiple commands with && or using backslash \ to write them on multiple lines. This way, Docker executes all commands in one RUN instruction, creating a single image layer. This keeps the image smaller and more efficient. The execution table shows each command running step by step, with the final layer created after all succeed. If any command fails, the build stops immediately. This method is common to optimize Docker images.

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