0
0
Jenkinsdevops~20 mins

Building Docker images in pipeline in Jenkins - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Docker Pipeline Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of Docker build command in Jenkins pipeline
What will be the output of the following Jenkins pipeline snippet when building a Docker image?
Jenkins
pipeline {
  agent any
  stages {
    stage('Build Docker Image') {
      steps {
        script {
          def image = docker.build('myapp:latest')
          echo "Image built: ${image.id}"
        }
      }
    }
  }
}
AImage built: <empty string>
BImage built: myapp:latest
CError: docker.build is not a valid method
DImage built: sha256:<image_id_hash>
Attempts:
2 left
💡 Hint
The docker.build method returns an image object with an id property representing the image hash.
Configuration
intermediate
2:00remaining
Correct Jenkinsfile syntax for building and pushing Docker image
Which Jenkinsfile snippet correctly builds a Docker image and pushes it to Docker Hub?
A
pipeline {
  agent any
  stages {
    stage('Build and Push') {
      steps {
        script {
          def img = docker.build('myuser/myapp:latest')
          img.push('latest')
        }
      }
    }
  }
}
B
pipeline {
  agent any
  stages {
    stage('Build and Push') {
      steps {
        docker.build('myuser/myapp:latest')
        docker.push('myuser/myapp:latest')
      }
    }
  }
}
C
pipeline {
  agent any
  stages {
    stage('Build and Push') {
      steps {
        script {
          docker.withRegistry('https://registry.hub.docker.com', 'dockerhub-credentials') {
            def img = docker.build('myuser/myapp:latest')
            img.push()
          }
        }
      }
    }
  }
}
D
pipeline {
  agent any
  stages {
    stage('Build and Push') {
      steps {
        script {
          def img = docker.build('myuser/myapp')
          img.push()
        }
      }
    }
  }
}
Attempts:
2 left
💡 Hint
Pushing requires authentication and the use of docker.withRegistry block with credentials.
Troubleshoot
advanced
2:00remaining
Troubleshooting Docker build failure in Jenkins pipeline
A Jenkins pipeline fails with the error: 'Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?'. What is the most likely cause?
AThe Jenkins agent does not have Docker installed or running.
BThe Jenkinsfile has syntax errors in the pipeline script.
CThe Dockerfile syntax is incorrect causing build failure.
DThe Docker image name is invalid.
Attempts:
2 left
💡 Hint
The error message indicates a connection problem to the Docker daemon socket.
🔀 Workflow
advanced
2:00remaining
Order of steps to build and deploy Docker image in Jenkins pipeline
What is the correct order of steps in a Jenkins pipeline to build a Docker image and deploy it to a Kubernetes cluster?
A4,1,2,3
B4,2,1,3
C1,4,2,3
D1,2,4,3
Attempts:
2 left
💡 Hint
Authentication must happen before build and push steps.
Best Practice
expert
2:00remaining
Best practice for caching Docker layers in Jenkins pipeline builds
Which approach is best to speed up Docker image builds in Jenkins pipelines by caching layers effectively?
AAlways use the --no-cache option to ensure fresh builds
BUse a remote Docker registry as a cache source and pull existing layers before building
CDelete all images on the Jenkins agent before each build to avoid conflicts
DBuild images without tagging to avoid cache pollution
Attempts:
2 left
💡 Hint
Caching layers reduces build time by reusing unchanged parts of the image.