Bird
Raised Fist0
Dockerdevops~10 mins

Building images with docker build - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - Building images with docker build
Write Dockerfile
↓
Run 'docker build'
↓
Docker reads Dockerfile
↓
Execute each instruction step-by-step
↓
Create image layers
↓
Tag image with name:tag
↓
Image ready to use
This flow shows how Docker reads your Dockerfile, executes each instruction to create layers, and builds a final image tagged for use.
Execution Sample
Docker
FROM alpine:3.18
RUN apk add --no-cache curl
CMD ["curl", "--version"]
This Dockerfile builds an image based on Alpine Linux, installs curl, and sets the default command to show curl version.
Process Table
StepDockerfile InstructionActionResultImage Layer Created
1FROM alpine:3.18Pull base image alpine:3.18 if not localBase image alpine:3.18 readyLayer 1: base alpine
2RUN apk add --no-cache curlRun apk command inside containercurl installed in imageLayer 2: curl installed
3CMD ["curl", "--version"]Set default command for containerDefault command setNo new layer (metadata)
4docker build completesImage built and taggedImage ready to runFinal image composed of layers 1 and 2 plus metadata
💡 All Dockerfile instructions processed, image built successfully
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
Image Layers[][base alpine][base alpine, curl installed][base alpine, curl installed][base alpine, curl installed]
Default CommandNoneNoneNone["curl", "--version"]["curl", "--version"]
Key Moments - 3 Insights
Why does the CMD instruction not create a new image layer?
CMD sets metadata for the container's default command and does not change the filesystem, so no new layer is created. See execution_table step 3.
What happens if the base image alpine:3.18 is already on the system?
Docker skips pulling the base image and uses the local copy, speeding up build. This is implied in execution_table step 1.
How does Docker build handle each RUN instruction?
Each RUN instruction executes commands inside a temporary container and commits the changes as a new image layer. See execution_table step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what image layers exist after step 2?
A[base alpine]
B[base alpine, curl installed]
C[curl installed]
DNo layers created yet
💡 Hint
Check the 'Image Layer Created' column for step 2 in the execution_table
At which step does Docker set the default command for the container?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at the 'Dockerfile Instruction' and 'Action' columns in execution_table
If you add another RUN instruction after step 2, how would the variable 'Image Layers' change after that step?
AIt stays the same
BThe base alpine layer is replaced
CA new layer is added to the list
DThe default command changes
💡 Hint
Refer to variable_tracker and key_moments about RUN instructions creating new layers
Concept Snapshot
docker build reads a Dockerfile line by line
Each instruction creates a new image layer except metadata commands like CMD
RUN executes commands inside a container and commits changes as a layer
FROM sets the base image
The final image is tagged and ready to run containers
Full Transcript
Building images with docker build starts by writing a Dockerfile with instructions like FROM, RUN, and CMD. When you run 'docker build', Docker reads the Dockerfile step-by-step. It pulls the base image if needed, then executes RUN commands inside temporary containers, creating new image layers for each. CMD sets the default command but does not create a new layer. After processing all instructions, Docker combines the layers and metadata into a final image tagged for use. This process allows you to create custom images with your software and settings ready to run.

Practice

(1/5)
1. What does the docker build command do?
easy
A. It creates a Docker image from a Dockerfile and context files.
B. It runs a Docker container from an image.
C. It deletes unused Docker images.
D. It lists all running Docker containers.

Solution

  1. Step 1: Understand the purpose of docker build

    The docker build command reads instructions from a Dockerfile and packages files from the build context into a new image.
  2. Step 2: Differentiate from other Docker commands

    Running containers, deleting images, or listing containers are done by other commands like docker run, docker rmi, and docker ps.
  3. Final Answer:

    It creates a Docker image from a Dockerfile and context files. -> Option A
  4. Quick Check:

    docker build = create image [OK]
Hint: Remember: build = create image from Dockerfile [OK]
Common Mistakes:
  • Confusing build with run command
  • Thinking build deletes images
  • Mixing build with container listing
2. Which of the following is the correct syntax to build a Docker image named myapp from the current directory?
easy
A. docker build . -name myapp
B. docker build myapp -t .
C. docker build -t myapp .
D. docker build --image myapp .

Solution

  1. Step 1: Identify the correct order and options for docker build

    The correct syntax uses -t to tag the image name, followed by the build context (here, current directory .).
  2. Step 2: Check each option for syntax correctness

    docker build -t myapp . correctly places -t myapp before the context .. Options A, B, and D misuse flags or order.
  3. Final Answer:

    docker build -t myapp . -> Option C
  4. Quick Check:

    docker build -t name context [OK]
Hint: Use -t before context folder to name image [OK]
Common Mistakes:
  • Placing image name after context
  • Using wrong flags like --image or -name
  • Omitting the build context folder
3. Given this Dockerfile in the current directory:
FROM alpine
RUN echo "Hello Docker" > /hello.txt

What will be the output of docker build -t testimage . if successful?
medium
A. An image named testimage is created with a file /hello.txt containing 'Hello Docker'.
B. A container runs and prints 'Hello Docker' to the terminal.
C. The build fails because echo is not a valid Docker command.
D. An image is created but /hello.txt is empty.

Solution

  1. Step 1: Understand Dockerfile instructions

    FROM alpine sets base image; RUN echo "Hello Docker" > /hello.txt creates a file inside the image.
  2. Step 2: Interpret the build command output

    The build creates an image named testimage with the file /hello.txt containing the text 'Hello Docker'. No container runs or prints output during build.
  3. Final Answer:

    An image named testimage is created with a file /hello.txt containing 'Hello Docker'. -> Option A
  4. Quick Check:

    RUN creates files in image, no output printed [OK]
Hint: RUN commands modify image, they don't print output during build [OK]
Common Mistakes:
  • Thinking RUN prints to terminal during build
  • Confusing image creation with container run
  • Assuming build fails due to shell commands
4. You run docker build -t myapp . but get the error: failed to read dockerfile: open Dockerfile: no such file or directory. What is the most likely cause?
medium
A. The build context folder is empty.
B. The -t option is used incorrectly.
C. Docker daemon is not running.
D. The Dockerfile is missing from the current directory.

Solution

  1. Step 1: Analyze the error message

    The error says Docker cannot find the Dockerfile in the build context folder (current directory).
  2. Step 2: Identify the cause

    If the Dockerfile is missing, Docker cannot build the image. The -t option and daemon status do not cause this specific error.
  3. Final Answer:

    The Dockerfile is missing from the current directory. -> Option D
  4. Quick Check:

    Missing Dockerfile = build fails with file not found [OK]
Hint: Check Dockerfile exists in build folder if build fails [OK]
Common Mistakes:
  • Assuming wrong flag causes error
  • Not verifying Dockerfile presence
  • Ignoring daemon status for this error
5. You want to build a Docker image named webapp:v1 using a Dockerfile located in ./app folder, but your current directory is the parent folder. Which command correctly builds the image?
hard
A. docker build -t webapp:v1 ./app
B. docker build -t webapp:v1 -f ./app/Dockerfile ./app
C. docker build ./app -t webapp:v1
D. docker build -f app/Dockerfile -t webapp:v1 ./app

Solution

  1. Step 1: Understand build context and Dockerfile location

    The build context is the folder with the Dockerfile and files needed. Here, it's ./app.
  2. Step 2: Choose correct command syntax

    docker build -t webapp:v1 -f ./app/Dockerfile ./app uses -t webapp:v1 to tag, -f ./app/Dockerfile to specify Dockerfile path, and ./app as context, which is correct. docker build -t webapp:v1 ./app assumes Dockerfile is in context folder but does not specify path explicitly. docker build ./app -t webapp:v1 has wrong order. docker build -f app/Dockerfile -t webapp:v1 ./app has incomplete Dockerfile path.
  3. Final Answer:

    docker build -t webapp:v1 -f ./app/Dockerfile ./app -> Option B
  4. Quick Check:

    Tag with -t, specify Dockerfile with -f, context last [OK]
Hint: Tag with -t, specify Dockerfile path with -f, context last [OK]
Common Mistakes:
  • Wrong order of options and context
  • Incorrect Dockerfile path with -f
  • Using current folder when Dockerfile is elsewhere
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