0
0
Dockerdevops~10 mins

RUN instruction for executing commands in Docker - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - RUN instruction for executing commands
Dockerfile with RUN
Docker build starts
RUN command executed in container
Changes saved as new image layer
Build continues or ends
The RUN instruction executes commands inside a temporary container during image build, saving changes as a new image layer.
Execution Sample
Docker
FROM ubuntu:latest
RUN apt-get update && apt-get install -y curl
RUN echo "Hello Docker" > /hello.txt
This Dockerfile updates packages, installs curl, and creates a file inside the image.
Process Table
StepRUN CommandExecution ResultImage Layer Created
1apt-get update && apt-get install -y curlPackages updated and curl installedLayer 1 created with updated packages and curl
2echo "Hello Docker" > /hello.txtFile /hello.txt created with textLayer 2 created with new file
💡 All RUN commands executed, image layers created, build proceeds or finishes
Status Tracker
VariableStartAfter Step 1After Step 2
Image LayersBase ubuntu imageBase + updated packages + curlPrevious + /hello.txt file
Key Moments - 2 Insights
Why does each RUN create a new image layer?
Each RUN command executes in a temporary container and its changes are saved as a new layer, as shown in execution_table rows 1 and 2.
What happens if a RUN command fails?
The build stops immediately at that RUN step, so no further layers are created after the failure step.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is created after the first RUN command?
AA new image layer with updated packages and curl
BA file named /hello.txt
CThe final Docker image
DNo changes yet
💡 Hint
Check execution_table row 1 under 'Image Layer Created'
At which step is the file /hello.txt created inside the image?
AStep 1
BStep 2
CBefore build starts
DAfter build finishes
💡 Hint
Look at execution_table row 2 under 'RUN Command' and 'Execution Result'
If the first RUN command fails, what happens to the image layers?
AOnly the second layer is created
BBoth layers are created anyway
COnly the base image exists, no new layers
DThe build completes successfully
💡 Hint
Refer to key_moments about build stopping on RUN failure
Concept Snapshot
RUN instruction executes commands during image build
Each RUN creates a new image layer with changes
Commands run in a temporary container
Failed RUN stops the build immediately
Use RUN to install packages or create files
Full Transcript
The RUN instruction in a Dockerfile runs commands inside a temporary container during the image build process. Each RUN command creates a new image layer that saves the changes made by that command. For example, running 'apt-get update && apt-get install -y curl' updates packages and installs curl, creating a new layer. Another RUN command can create files or configure the image further. If any RUN command fails, the build stops immediately and no further layers are created. This process helps build images step-by-step with each RUN adding a layer.