Bird
Raised Fist0
Jenkinsdevops~10 mins

Docker image as artifact in Jenkins - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - Docker image as artifact
Start Jenkins Job
↓
Checkout Code
↓
Build Docker Image
↓
Tag Docker Image
↓
Push Image to Registry
↓
Use Image as Artifact in Next Steps
↓
End
This flow shows how Jenkins builds a Docker image, tags it, pushes it to a registry, and uses it as an artifact for later steps.
Execution Sample
Jenkins
pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        script {
          sh 'docker build -t myapp:${BUILD_NUMBER} .'
          sh 'docker push myapp:${BUILD_NUMBER}'
        }
      }
    }
  }
}
This Jenkins pipeline builds a Docker image tagged with the build number and pushes it to a Docker registry.
Process Table
StepActionCommand RunResultNext Step
1Start Jenkins JobN/AJob triggeredCheckout Code
2Checkout Codegit clone repoCode available locallyBuild Docker Image
3Build Docker Imagedocker build -t myapp:1 .Image 'myapp:1' createdTag Docker Image
4Tag Docker ImageImplicit in build commandImage tagged as 'myapp:1'Push Image to Registry
5Push Image to Registrydocker push myapp:1Image pushed to registryUse Image as Artifact
6Use Image as ArtifactN/AImage ready for deployment or next stepsEnd
7EndN/APipeline finished successfullyN/A
💡 Pipeline ends after image is pushed and ready as artifact for next steps.
Status Tracker
VariableStartAfter Step 3After Step 5Final
BUILD_NUMBER1111
Docker Image Tagnonemyapp:1myapp:1myapp:1
Image in Registrynonoyesyes
Key Moments - 3 Insights
Why do we tag the Docker image with ${BUILD_NUMBER}?
Tagging with ${BUILD_NUMBER} creates a unique image version for each build, as shown in execution_table step 3 and 4, preventing overwriting previous images.
What happens if the docker push fails?
If docker push fails (step 5), the image is not stored in the registry and cannot be used as an artifact, so the pipeline usually stops or retries.
Is the image available locally after the build?
Yes, after step 3, the image is built and stored locally on the Jenkins agent before pushing to the registry.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the Docker image tag after step 3?
Amyapp:latest
Blatest
Cmyapp:1
Dnone
💡 Hint
Check the 'Result' column in step 3 of the execution_table.
At which step is the Docker image pushed to the registry?
AStep 5
BStep 3
CStep 4
DStep 6
💡 Hint
Look for the 'Push Image to Registry' action in the execution_table.
If BUILD_NUMBER was '2', how would the Docker image tag change after step 3?
Amyapp:1
Bmyapp:2
Cmyapp:latest
Dmyapp
💡 Hint
Refer to variable_tracker for BUILD_NUMBER and Docker Image Tag values.
Concept Snapshot
Jenkins builds Docker images tagged uniquely (e.g., with BUILD_NUMBER).
The image is pushed to a Docker registry.
This image acts as an artifact for deployment or later pipeline stages.
Tagging prevents overwriting previous images.
Push must succeed for artifact availability.
Full Transcript
This visual execution shows how Jenkins builds a Docker image as an artifact. The pipeline starts by checking out code, then builds a Docker image tagged with the build number. The image is stored locally and then pushed to a Docker registry. Once pushed, the image is available as an artifact for deployment or further pipeline steps. Tagging with the build number ensures each image is unique. If pushing fails, the image is not available remotely. This process helps manage Docker images reliably in CI/CD pipelines.

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

  1. 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.
  2. Step 2: Identify Jenkins artifact purpose

    Jenkins artifacts store files from builds for later use or sharing, perfect for Docker images.
  3. Final Answer:

    To reuse the image later without rebuilding it -> Option D
  4. 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

  1. Step 1: Recall Jenkins archiveArtifacts syntax

    The correct syntax uses the parameter name 'artifacts' to specify files to archive.
  2. Step 2: Match correct parameter and spelling

    archiveArtifacts artifacts: 'myimage.tar' uses 'archiveArtifacts artifacts: 'myimage.tar'', which is correct syntax.
  3. Final Answer:

    archiveArtifacts artifacts: 'myimage.tar' -> Option A
  4. 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

  1. 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.
  2. Step 2: Understand archiveArtifacts usage

    The 'archiveArtifacts' step archives the saved file as a Jenkins build artifact for later use.
  3. Final Answer:

    Save the Docker image as a file and archive it as a build artifact -> Option B
  4. 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

  1. Step 1: Check archiveArtifacts parameter

    The correct parameter to specify files is 'artifacts', not 'file'. Using 'file' causes failure.
  2. Step 2: Confirm docker save output

    'docker save' correctly creates the file 'myapp.tar', so the problem is not with the file creation.
  3. Final Answer:

    Because the parameter name should be 'artifacts', not 'file' -> Option C
  4. 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

  1. Step 1: Understand docker load syntax

    To load an image from a file, use 'docker load -i filename'. This imports the image into Docker.
  2. Step 2: Run the loaded image

    After loading, run the image by its tag, e.g., 'docker run appimage:latest'.
  3. Final Answer:

    sh 'docker load -i appimage.tar' sh 'docker run appimage:latest' -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard