📖 Scenario: You work in a team that uses Jenkins to automate software builds. Your task is to create a Jenkins pipeline script that builds a Docker image from a simple Dockerfile. This helps your team package applications consistently.
🎯 Goal: Build a Jenkins pipeline script that defines a Docker image build step using a Dockerfile in the project folder. The pipeline should build the image with a specific name.
📋 What You'll Learn
Create a Jenkins pipeline script with a pipeline block
Define an agent to run the pipeline on any available node
Add a stage named Build Docker Image
Inside the stage, run a shell command to build a Docker image named myapp:latest using the Dockerfile in the current directory
Print a message after the build completes
💡 Why This Matters
🌍 Real World
Teams use Jenkins pipelines to automate building Docker images for consistent application packaging and deployment.
💼 Career
Knowing how to write Jenkins pipelines that build Docker images is a key skill for DevOps engineers and automation specialists.
Progress0 / 4 steps
1
Create the Jenkins pipeline skeleton
Write a Jenkins pipeline script starting with pipeline block and an agent any declaration.
Jenkins
Hint
Start by writing pipeline { and inside it add agent any to run on any node.
2
Add a stage to build the Docker image
Inside the pipeline block, add a stages block with one stage named Build Docker Image.
Jenkins
Hint
Add stages {} inside pipeline. Then add a stage('Build Docker Image') with steps {} inside.
3
Add the Docker build shell command
Inside the steps block of the Build Docker Image stage, add a sh step that runs docker build -t myapp:latest . to build the Docker image.
Jenkins
Hint
Use sh 'docker build -t myapp:latest .' inside steps to run the Docker build command.
4
Print a message after building the image
After the sh command inside the steps block, add a echo step that prints 'Docker image myapp:latest built successfully.'.
Jenkins
Hint
Use echo 'Docker image myapp:latest built successfully.' after the shell command to print the message.
Practice
(1/5)
1. What is the main purpose of using docker.build() in a Jenkins pipeline?
easy
A. To run tests inside a Docker container
B. To deploy a Docker container to a cloud service
C. To remove unused Docker images from the system
D. To create a Docker image from a Dockerfile during the pipeline execution
Solution
Step 1: Understand the role of docker.build()
This command is used to build Docker images from a Dockerfile within a Jenkins pipeline.
Step 2: Differentiate from other Docker commands
It does not deploy containers, remove images, or run tests directly; it only builds images.
Final Answer:
To create a Docker image from a Dockerfile during the pipeline execution -> Option D
Quick Check:
docker.build() builds images [OK]
Hint: Remember: docker.build() creates images, not containers [OK]
Common Mistakes:
Confusing image build with container deployment
Thinking docker.build() removes images
Assuming docker.build() runs tests
2. Which of the following is the correct syntax to build a Docker image named myapp inside a Jenkins pipeline script block?
easy
A. docker.build image: 'myapp'
B. docker.build('image=myapp')
C. docker.build('myapp')
D. docker.build('build myapp')
Solution
Step 1: Recall the correct method signature
The docker.build() method takes the image name as a string argument, e.g., docker.build('myapp').
Step 2: Identify incorrect syntax
Options A, C, and D use invalid syntax or incorrect parameter formats.
Final Answer:
docker.build('myapp') -> Option C
Quick Check:
Correct syntax is docker.build('imageName') [OK]
Hint: Use docker.build('imageName') inside script block [OK]
Common Mistakes:
Using key-value syntax incorrectly
Adding extra words inside the string
Missing quotes around image name
3. Given this Jenkins pipeline snippet inside a script block:
Blaming Docker installation without checking syntax
5. You want to build a Docker image in Jenkins pipeline and tag it with the current Git commit hash. Which snippet correctly achieves this inside a script block?