0
0
Jenkinsdevops~20 mins

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

Choose your learning style9 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.