0
0
Dockerdevops~10 mins

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

Choose your learning style9 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.