📖 Scenario: You are working as a DevOps engineer. Your team uses Jenkins to automate building and pushing Docker images to a container registry. You want to create a simple Jenkins pipeline script that builds a Docker image from a Dockerfile and pushes it to a registry.
🎯 Goal: Build a Jenkins pipeline script that builds a Docker image named myapp with tag v1 and pushes it to the Docker registry at docker.io/myuser/myapp:v1.
📋 What You'll Learn
Create a variable for the Docker image name
Create a variable for the Docker image tag
Use the docker.build command to build the image
Use the docker.withRegistry block to push the image
Print a message after pushing the image
💡 Why This Matters
🌍 Real World
Automating Docker image builds and pushes is common in continuous integration pipelines to ensure consistent deployments.
💼 Career
DevOps engineers and automation specialists use Jenkins pipelines to manage container images and deploy applications efficiently.
Progress0 / 4 steps
1
Set Docker image name and tag
Create two variables in the Jenkins pipeline script: imageName set to myapp and imageTag set to v1.
Jenkins
Hint
Use def to declare variables in Jenkins scripted pipeline.
2
Build the Docker image
Use the docker.build command to build the Docker image with the name "${imageName}:${imageTag}" and assign it to a variable called appImage.
Jenkins
Hint
Use docker.build with the image name and tag inside double quotes and curly braces.
3
Push the Docker image to registry
Use docker.withRegistry with the URL 'https://docker.io' and credentials ID 'dockerhub-creds' to push the image stored in appImage.
Jenkins
Hint
Wrap the push command inside docker.withRegistry block with the registry URL and credentials ID.
4
Print confirmation message
Add a println statement to print "Docker image pushed: myapp:v1" after the image is pushed.
Jenkins
Hint
Use println with the exact message including image name and tag.
Practice
(1/5)
1. What is the main purpose of pushing a Docker image to a registry in Jenkins?
easy
A. To run the Docker image on the Jenkins server
B. To delete the local Docker image after building
C. To create a new Dockerfile automatically
D. To save the built image so it can be shared and deployed elsewhere
Solution
Step 1: Understand Docker image pushing
Pushing an image uploads it to a registry so others can access it.
Step 2: Connect to Jenkins usage
Jenkins automates this to share images for deployment or other jobs.
Final Answer:
To save the built image so it can be shared and deployed elsewhere -> Option D
Quick Check:
Pushing = Save and share image [OK]
Hint: Pushing means uploading image for sharing [OK]
Common Mistakes:
Confusing push with running the image
Thinking push deletes local images
Assuming push creates Dockerfiles
2. Which Jenkins pipeline step correctly pushes a Docker image named myapp:latest to Docker Hub after login?
easy
A. sh 'docker push myapp:latest'
B. sh 'docker pull myapp:latest'
C. sh 'docker build myapp:latest'
D. sh 'docker run myapp:latest'
Solution
Step 1: Identify the push command
To upload an image, the command is docker push followed by the image name and tag.
Step 2: Check other commands
docker pull downloads, docker build creates, and docker run executes images.
Final Answer:
sh 'docker push myapp:latest' -> Option A
Quick Check:
Push uploads image [OK]
Hint: Push uses 'docker push' command [OK]
Common Mistakes:
Using pull instead of push
Confusing build with push
Trying to run instead of push
3. Given this Jenkins pipeline snippet, what will be the output if the Docker login fails?
stage('Push Image') {
steps {
sh 'docker login -u user -p wrongpassword'
sh 'docker push myapp:latest'
}
}
medium
A. The image will be pushed anonymously
B. The push will succeed anyway
C. The push will fail because login failed
D. The pipeline will skip the push step
Solution
Step 1: Understand docker login failure effect
If login fails, Docker does not authenticate, so push cannot succeed to a private registry.
Step 2: Check pipeline behavior
Since login command fails, the push command will also fail due to lack of permissions.
Final Answer:
The push will fail because login failed -> Option C