Bird
Raised Fist0
Jenkinsdevops~20 mins

Docker image as artifact in Jenkins - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Docker Image Artifact Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this Jenkins pipeline snippet?

Consider this Jenkins pipeline step that builds a Docker image and tags it:

stage('Build Docker Image') {
  steps {
    script {
      dockerImage = docker.build("myapp:${env.BUILD_NUMBER}")
      echo dockerImage.id
    }
  }
}

What will be printed by echo dockerImage.id?

Jenkins
stage('Build Docker Image') {
  steps {
    script {
      dockerImage = docker.build("myapp:${env.BUILD_NUMBER}")
      echo dockerImage.id
    }
  }
}
AAn error because dockerImage.id is not defined
BThe string 'myapp:${env.BUILD_NUMBER}' literally
CThe Jenkins build number only
DThe unique image ID hash of the built Docker image
Attempts:
2 left
💡 Hint

Think about what docker.build() returns and what id represents.

🔀 Workflow
intermediate
2:00remaining
Which Jenkins pipeline step correctly pushes a Docker image as an artifact to Docker Hub?

You have built a Docker image in Jenkins with tag myapp:${env.BUILD_NUMBER}. Which step correctly pushes this image to Docker Hub?

Ash 'docker push myapp:${env.BUILD_NUMBER}'
Bdocker.withRegistry('https://registry.hub.docker.com', 'dockerhub-credentials') { dockerImage.push() }
Cdocker.push('myapp:${env.BUILD_NUMBER}')
DdockerImage.push('latest')
Attempts:
2 left
💡 Hint

Remember Jenkins needs to authenticate to Docker Hub before pushing.

❓ Troubleshoot
advanced
2:00remaining
Why does this Jenkins pipeline fail to push the Docker image?

Given this snippet:

stage('Push Image') {
  steps {
    script {
      dockerImage = docker.build("myapp:${env.BUILD_NUMBER}")
      dockerImage.push()
    }
  }
}

The pipeline fails with an authentication error. What is the most likely cause?

AMissing <code>docker.withRegistry</code> block to provide Docker Hub credentials
BThe image tag is invalid and causes push failure
CThe <code>docker.build</code> command did not complete successfully
DThe Jenkins agent does not have Docker installed
Attempts:
2 left
💡 Hint

Think about how Jenkins authenticates to Docker registries.

🧠 Conceptual
advanced
2:00remaining
What is the main benefit of using Docker images as artifacts in Jenkins pipelines?

Why do DevOps teams prefer storing Docker images as build artifacts in Jenkins pipelines?

AIt ensures consistent deployment by using the exact same image built and tested
BIt reduces the size of the Jenkins workspace by deleting source code
CIt speeds up the Jenkins build by skipping tests
DIt allows Jenkins to run containers without Docker installed on agents
Attempts:
2 left
💡 Hint

Think about consistency and reliability in deployments.

✅ Best Practice
expert
3:00remaining
Which Jenkinsfile snippet follows best practices for building, tagging, and pushing a Docker image as an artifact?

Choose the Jenkins pipeline snippet that correctly builds a Docker image, tags it with the build number and 'latest', pushes both tags to Docker Hub, and uses credentials securely.

A
stage('Build and Push') {
  steps {
    script {
      dockerImage = docker.build("myapp:${env.BUILD_NUMBER}")
      dockerImage.push('latest')
      dockerImage.push()
    }
  }
}
B
stage('Build and Push') {
  steps {
    sh 'docker build -t myapp:${env.BUILD_NUMBER} .'
    sh 'docker push myapp:${env.BUILD_NUMBER}'
    sh 'docker push myapp:latest'
  }
}
C
stage('Build and Push') {
  steps {
    script {
      dockerImage = docker.build("myapp:${env.BUILD_NUMBER}")
      docker.withRegistry('https://registry.hub.docker.com', 'dockerhub-credentials') {
        dockerImage.push()
        dockerImage.push('latest')
      }
    }
  }
}
D
stage('Build and Push') {
  steps {
    script {
      dockerImage = docker.build("myapp:latest")
      dockerImage.push()
      dockerImage.push('${env.BUILD_NUMBER}')
    }
  }
}
Attempts:
2 left
💡 Hint

Look for correct tag usage, push order, and secure credential handling.

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