📖 Scenario: You work in a team that uses Docker containers to package applications. Your team wants to automate building Docker images whenever new code is pushed. This helps deliver updates faster and with fewer errors.
🎯 Goal: Build a simple Docker image using a Dockerfile and automate the build process in a CI pipeline script.
📋 What You'll Learn
Create a Dockerfile with a base image and a simple command
Add a variable for the image tag
Write a command to build the Docker image using the tag variable
Print the Docker build command output
💡 Why This Matters
🌍 Real World
Automating Docker image builds in CI pipelines helps teams deliver software updates quickly and reliably.
💼 Career
Knowing how to build Docker images and automate builds is a key skill for DevOps engineers and developers working with containers.
Progress0 / 4 steps
1
Create a Dockerfile
Create a file named Dockerfile with these exact contents: FROM alpine:3.18 CMD ["echo", "Hello from Docker image"]
Docker
Hint
The Dockerfile must start with FROM alpine:3.18 and have a CMD that echoes the exact text.
2
Add an image tag variable
In a shell script file named build.sh, create a variable called IMAGE_TAG and set it to myapp:latest.
Docker
Hint
Use IMAGE_TAG=myapp:latest exactly in your script.
3
Write Docker build command
In the build.sh script, add a command to build the Docker image using the IMAGE_TAG variable and the current directory as context. Use docker build -t $IMAGE_TAG ..
Docker
Hint
Use the exact command docker build -t $IMAGE_TAG . to build the image.
4
Print build command output
Add a command to the build.sh script to print the message Docker image built with tag: $IMAGE_TAG after the build command.
Docker
Hint
Use echo "Docker image built with tag: $IMAGE_TAG" to print the message.
Practice
(1/5)
1. What is the main purpose of building Docker images in a CI pipeline?
easy
A. To automate packaging the app for consistent deployment
B. To manually upload files to the server
C. To write application code faster
D. To monitor server health
Solution
Step 1: Understand CI pipeline role
CI pipelines automate tasks like testing and packaging to ensure consistency.
Step 2: Identify Docker image purpose
Docker images package the app and its environment for reliable deployment.
Final Answer:
To automate packaging the app for consistent deployment -> Option A
Quick Check:
CI builds images to package apps consistently [OK]
Hint: CI builds images to package apps consistently [OK]
Common Mistakes:
Confusing image building with code writing
Thinking CI uploads files manually
Mixing deployment with monitoring
2. Which of the following is the correct command to build a Docker image named myapp with tag v1 from the current directory?
easy
A. docker build -t myapp:v1 .
B. docker create -t myapp:v1 .
C. docker run -t myapp:v1 .
D. docker push -t myapp:v1 .
Solution
Step 1: Identify Docker build syntax
The correct syntax to build an image is docker build -t name:tag path.
Step 2: Match command to syntax
docker build -t myapp:v1 . uses docker build -t myapp:v1 . which matches the syntax and current directory.
Hint: Use 'docker build -t name:tag path' to build images [OK]
Common Mistakes:
Using 'docker create' instead of 'docker build'
Confusing 'docker run' with building images
Trying to push before building
3. Given this CI script snippet:
docker build -t myapp:latest .
docker tag myapp:latest myapp:build-123
What will be the result after running these commands?
medium
A. An error because tagging must happen before building
B. Only one image named 'myapp:build-123' exists
C. Two images: 'myapp:latest' and 'myapp:build-123' pointing to the same image
D. Two images with different contents
Solution
Step 1: Understand docker build output
The build creates an image tagged 'myapp:latest'.
Step 2: Understand docker tag effect
Tagging 'myapp:latest' as 'myapp:build-123' creates a new tag pointing to the same image.
Final Answer:
Two images: 'myapp:latest' and 'myapp:build-123' pointing to the same image -> Option C
Quick Check:
Tag duplicates image reference, no new image created [OK]
Hint: Tagging duplicates image reference, no new image built [OK]
Common Mistakes:
Thinking tagging creates a new image
Assuming error if tag after build
Believing images have different contents after tag
4. A CI pipeline script has this line:
docker build -t myapp .
But the build fails with error: "Cannot locate Dockerfile". What is the likely cause?
medium
A. The tag name 'myapp' is invalid
B. Dockerfile is missing in the current directory
C. The docker daemon is not running
D. The build command syntax is wrong
Solution
Step 1: Analyze error message
"Cannot locate Dockerfile" means Docker can't find the Dockerfile in the build context.
Step 2: Check build context and files
Since the path is '.', Docker expects a Dockerfile in the current directory, which is missing.
Final Answer:
Dockerfile is missing in the current directory -> Option B
Quick Check:
Missing Dockerfile causes this error [OK]
Hint: Check Dockerfile presence in build directory [OK]
Common Mistakes:
Blaming tag name for Dockerfile error
Assuming daemon not running without checking
Thinking build syntax is incorrect
5. In a CI pipeline, you want to tag Docker images with the build number stored in the environment variable BUILD_ID. Which command correctly builds and tags the image myapp with this dynamic tag?
hard
A. docker build -t 'myapp:${BUILD_ID}' .
B. docker build -t myapp:BUILD_ID .
C. docker build -t myapp:$(BUILD_ID) .
D. docker build -t myapp:$BUILD_ID .
Solution
Step 1: Understand shell variable syntax
In shell scripts, environment variables are accessed with $VAR or ${VAR}.
Step 2: Identify correct variable usage in command
docker build -t myapp:$BUILD_ID . uses $BUILD_ID which is valid and common in CI scripts.
Final Answer:
docker build -t myapp:$BUILD_ID . -> Option D
Quick Check:
Use $VAR to insert env variable in commands [OK]
Hint: Use $VAR to insert env variable in commands [OK]
Common Mistakes:
Using literal 'BUILD_ID' instead of variable
Using incorrect syntax like $(BUILD_ID)
Forgetting to expand variable in tag
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.