📖 Scenario: You are working as a DevOps engineer. Your team wants to build a Docker image from a simple Dockerfile and save this image as an artifact in Jenkins. This will help the team reuse the image later without rebuilding it every time.
🎯 Goal: Create a Jenkins pipeline script that builds a Docker image from a Dockerfile, tags it with a specific name, and archives the image as an artifact.
📋 What You'll Learn
Create a Dockerfile with a simple base image
Write a Jenkins pipeline script
Build a Docker image with a specific tag
Save the Docker image as a tar file
Archive the tar file as a Jenkins artifact
💡 Why This Matters
🌍 Real World
Saving Docker images as artifacts in Jenkins pipelines helps teams reuse images without rebuilding, speeding up deployments and ensuring consistency.
💼 Career
This skill is essential for DevOps engineers and CI/CD pipeline developers who automate container builds and deployments.
Progress0 / 4 steps
1
Create a simple Dockerfile
Create a file named Dockerfile with the following content exactly: FROM alpine:3.18 CMD ["echo", "Hello from Docker"]
Jenkins
Hint
This Dockerfile uses Alpine Linux as the base image and prints a message when run.
2
Define Docker image name variable in Jenkinsfile
In your Jenkins pipeline script, create a variable called imageName and set it to myapp:test.
Jenkins
Hint
Use def imageName = 'myapp:test' to define the variable.
3
Build and save Docker image in Jenkins pipeline
Add a stage named 'Build' in the Jenkins pipeline that runs shell commands to build the Docker image using docker build -t myapp:test . and then save it as myapp_test.tar using docker save -o myapp_test.tar myapp:test.
Jenkins
Hint
Use sh steps inside the stage('Build') to run the Docker commands.
4
Archive the Docker image tar file as artifact
Add a stage named 'Archive' in the Jenkins pipeline that archives the file myapp_test.tar as an artifact using archiveArtifacts 'myapp_test.tar'.
Jenkins
Hint
Use archiveArtifacts 'myapp_test.tar' inside the stage('Archive') to save the artifact.
Practice
(1/5)
1. What is the main reason to save a Docker image as an artifact in Jenkins?
easy
A. To run the image on a different operating system
B. To delete the image after the build
C. To convert the image into a container
D. To reuse the image later without rebuilding it
Solution
Step 1: Understand Docker image reuse
Saving a Docker image as an artifact allows you to reuse it later without rebuilding, saving time and resources.
Step 2: Identify Jenkins artifact purpose
Jenkins artifacts store files from builds for later use or sharing, perfect for Docker images.
Final Answer:
To reuse the image later without rebuilding it -> Option D
Quick Check:
Docker image reuse = To reuse the image later without rebuilding it [OK]
Hint: Artifacts store files to reuse later, like saving your work [OK]
Common Mistakes:
Thinking artifacts delete images
Confusing image with container
Assuming artifacts change OS compatibility
2. Which Jenkins pipeline step correctly archives a saved Docker image file named myimage.tar?
easy
A. archiveArtifacts artifacts: 'myimage.tar'
B. archiveArtifacts file: 'myimage.tar'
C. archiveArtifact artifacts: 'myimage.tar'
D. archiveArtifacts files: 'myimage.tar'
Solution
Step 1: Recall Jenkins archiveArtifacts syntax
The correct syntax uses the parameter name 'artifacts' to specify files to archive.
Step 2: Match correct parameter and spelling
archiveArtifacts artifacts: 'myimage.tar' uses 'archiveArtifacts artifacts: 'myimage.tar'', which is correct syntax.
Final Answer:
archiveArtifacts artifacts: 'myimage.tar' -> Option A
Quick Check:
Correct parameter is 'artifacts' = archiveArtifacts artifacts: 'myimage.tar' [OK]
Hint: Use 'artifacts' parameter exactly in archiveArtifacts step [OK]
Common Mistakes:
Using 'file' instead of 'artifacts'
Misspelling 'archiveArtifacts'
Using singular 'archiveArtifact'
3. Given this Jenkins pipeline snippet:
stage('Save Image') {
steps {
sh 'docker save myapp:latest -o myapp.tar'
archiveArtifacts artifacts: 'myapp.tar'
}
}
What will Jenkins do after this stage completes?
medium
A. Delete the Docker image myapp:latest
B. Save the Docker image as a file and archive it as a build artifact
C. Run the Docker container from the saved image
D. Build a new Docker image named myapp:latest
Solution
Step 1: Analyze docker save command
The command 'docker save myapp:latest -o myapp.tar' saves the Docker image to a file named myapp.tar.
Step 2: Understand archiveArtifacts usage
The 'archiveArtifacts' step archives the saved file as a Jenkins build artifact for later use.
Final Answer:
Save the Docker image as a file and archive it as a build artifact -> Option B
Quick Check:
docker save + archiveArtifacts = Save the Docker image as a file and archive it as a build artifact [OK]
Hint: docker save creates file; archiveArtifacts stores it [OK]
Common Mistakes:
Confusing save with build
Thinking it runs containers
Assuming image is deleted
4. You wrote this Jenkins pipeline snippet:
sh 'docker save myapp:latest -o myapp.tar'
archiveArtifacts file: 'myapp.tar'
Why does Jenkins fail to archive the Docker image file?
medium
A. Because 'docker save' does not create a file
B. Because the image name is incorrect
C. Because the parameter name should be 'artifacts', not 'file'
D. Because archiveArtifacts cannot archive .tar files
Solution
Step 1: Check archiveArtifacts parameter
The correct parameter to specify files is 'artifacts', not 'file'. Using 'file' causes failure.
Step 2: Confirm docker save output
'docker save' correctly creates the file 'myapp.tar', so the problem is not with the file creation.
Final Answer:
Because the parameter name should be 'artifacts', not 'file' -> Option C
Quick Check:
Parameter must be 'artifacts' = Because the parameter name should be 'artifacts', not 'file' [OK]
Hint: Use 'artifacts' parameter exactly in archiveArtifacts step [OK]
Common Mistakes:
Using 'file' instead of 'artifacts'
Assuming docker save fails silently
Thinking archiveArtifacts rejects .tar files
5. You want to reuse a Docker image saved as appimage.tar from a previous Jenkins build artifact in a new pipeline. Which sequence correctly loads and runs the image?
hard
A. sh 'docker load -i appimage.tar'
sh 'docker run appimage:latest'
B. sh 'docker run -i appimage.tar'
sh 'docker load appimage:latest'
C. sh 'docker save -i appimage.tar'
sh 'docker run appimage:latest'
D. sh 'docker load appimage.tar'
sh 'docker run appimage:latest'
Solution
Step 1: Understand docker load syntax
To load an image from a file, use 'docker load -i filename'. This imports the image into Docker.
Step 2: Run the loaded image
After loading, run the image by its tag, e.g., 'docker run appimage:latest'.
Final Answer:
sh 'docker load -i appimage.tar'
sh 'docker run appimage:latest' -> Option A
Quick Check:
Load with '-i', then run by tag = sh 'docker load -i appimage.tar'
sh 'docker run appimage:latest' [OK]
Hint: Load with 'docker load -i', then run image by tag [OK]
Common Mistakes:
Using 'docker run' with file instead of image tag
Confusing 'docker save' with 'docker load'
Omitting '-i' option in docker load
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.