📖 Scenario: You are working as a DevOps engineer. Your team needs a Docker image for a simple web application. You will create a Dockerfile, set a base image, add a configuration, build the image, and then check the image is created.
🎯 Goal: Build a Docker image step-by-step using the docker build command and a Dockerfile. You will create the Dockerfile, add a base image, add a label, build the image with a tag, and finally list the images to confirm the build.
📋 What You'll Learn
Create a Dockerfile with the exact name Dockerfile
Use the base image alpine:3.18
Add a label maintainer="devops@example.com"
Build the Docker image with the tag mywebapp:1.0
List Docker images to confirm the new image exists
💡 Why This Matters
🌍 Real World
Building Docker images is a key step in packaging applications for deployment in containers. This project simulates creating a basic image for a web app.
💼 Career
DevOps engineers and developers use Docker images to create consistent environments. Knowing how to build and tag images is essential for container workflows.
Progress0 / 4 steps
1
Create a Dockerfile with a base image
Create a file named Dockerfile and write the line FROM alpine:3.18 to set the base image.
Docker
Hint
The first line in a Dockerfile usually starts with FROM followed by the base image name and tag.
2
Add a maintainer label to the Dockerfile
Add the line LABEL maintainer="devops@example.com" below the FROM alpine:3.18 line in the Dockerfile.
Docker
Hint
The LABEL instruction adds metadata to the image. Use the exact key and value as shown.
3
Build the Docker image with a tag
Run the command docker build -t mywebapp:1.0 . in the terminal to build the Docker image using the current directory's Dockerfile and tag it as mywebapp:1.0.
Docker
Hint
The docker build command builds an image from a Dockerfile. Use -t to tag the image with a name and version.
4
List Docker images to verify the build
Run the command docker images mywebapp:1.0 to list the Docker images and confirm that the image mywebapp:1.0 exists.
Docker
Hint
The docker images command shows all images. Filtering by the image name and tag helps confirm your build.
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
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.
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.
Final Answer:
It creates a Docker image from a Dockerfile and context files. -> Option A
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
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 .).
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.
Final Answer:
docker build -t myapp . -> Option C
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
Step 1: Understand Dockerfile instructions
FROM alpine sets base image; RUN echo "Hello Docker" > /hello.txt creates a file inside the image.
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.
Final Answer:
An image named testimage is created with a file /hello.txt containing 'Hello Docker'. -> Option A
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
Step 1: Analyze the error message
The error says Docker cannot find the Dockerfile in the build context folder (current directory).
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.
Final Answer:
The Dockerfile is missing from the current directory. -> Option D
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
Step 1: Understand build context and Dockerfile location
The build context is the folder with the Dockerfile and files needed. Here, it's ./app.
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.
Final Answer:
docker build -t webapp:v1 -f ./app/Dockerfile ./app -> Option B
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 unknownstrongwhile earning +1 Grip toward your certificate.