The docker build command creates a Docker image from a set of instructions in a Dockerfile. It packages your application and its environment into a reusable image.
Click to reveal answer
beginner
What file does docker build use by default to build an image?
By default, docker build looks for a file named Dockerfile in the specified build context directory.
Click to reveal answer
beginner
What does the -t option do in the docker build command?
The -t option tags the image with a name and optionally a version, making it easier to identify and run later.
Click to reveal answer
intermediate
Explain the term 'build context' in Docker image building.
The build context is the set of files and folders sent to the Docker daemon during the build. It includes the Dockerfile and any files referenced inside it.
Click to reveal answer
intermediate
Why is it important to keep the build context small?
A smaller build context speeds up the build process because fewer files are sent to the Docker daemon. It also reduces the chance of accidentally including sensitive or unnecessary files.
Click to reveal answer
What is the default filename that docker build looks for?
ADockerfile
Bdocker-compose.yml
Cbuildfile
Dimagefile
✗ Incorrect
The default file for docker build is Dockerfile.
Which option tags the image with a name during build?
A-t
B-b
C-i
D-n
✗ Incorrect
The -t option tags the image with a name and optional version.
What does the build context include?
AOnly the Dockerfile
BOnly files listed in .dockerignore
COnly files copied during the build
DAll files in the current directory and subdirectories
✗ Incorrect
The build context includes all files in the directory sent to Docker unless excluded by .dockerignore.
Why should you keep the build context small?
ATo reduce image size
BTo speed up the build process
CTo increase container startup time
DTo avoid using Dockerfile
✗ Incorrect
A smaller build context speeds up the build by sending fewer files to Docker.
Which command builds a Docker image from the current directory and tags it as 'myapp:latest'?
Adocker create -t myapp:latest .
Bdocker run -t myapp:latest .
Cdocker build -t myapp:latest .
Ddocker build -n myapp:latest .
✗ Incorrect
docker build -t myapp:latest . builds and tags the image from the current directory.
Describe the process of building a Docker image using docker build. Include what files are involved and how tagging works.
Think about the Dockerfile, the folder you build from, and how you name the image.
You got /4 concepts.
Explain why managing the build context is important when building Docker images.
Consider what happens when you send too many files to Docker during build.
You got /4 concepts.
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.