Bird
Raised Fist0
Jenkinsdevops~20 mins

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

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

Practice

(1/5)
1. What is the main purpose of using docker.build() in a Jenkins pipeline?
easy
A. To run tests inside a Docker container
B. To deploy a Docker container to a cloud service
C. To remove unused Docker images from the system
D. To create a Docker image from a Dockerfile during the pipeline execution

Solution

  1. Step 1: Understand the role of docker.build()

    This command is used to build Docker images from a Dockerfile within a Jenkins pipeline.
  2. Step 2: Differentiate from other Docker commands

    It does not deploy containers, remove images, or run tests directly; it only builds images.
  3. Final Answer:

    To create a Docker image from a Dockerfile during the pipeline execution -> Option D
  4. Quick Check:

    docker.build() builds images [OK]
Hint: Remember: docker.build() creates images, not containers [OK]
Common Mistakes:
  • Confusing image build with container deployment
  • Thinking docker.build() removes images
  • Assuming docker.build() runs tests
2. Which of the following is the correct syntax to build a Docker image named myapp inside a Jenkins pipeline script block?
easy
A. docker.build image: 'myapp'
B. docker.build('image=myapp')
C. docker.build('myapp')
D. docker.build('build myapp')

Solution

  1. Step 1: Recall the correct method signature

    The docker.build() method takes the image name as a string argument, e.g., docker.build('myapp').
  2. Step 2: Identify incorrect syntax

    Options A, C, and D use invalid syntax or incorrect parameter formats.
  3. Final Answer:

    docker.build('myapp') -> Option C
  4. Quick Check:

    Correct syntax is docker.build('imageName') [OK]
Hint: Use docker.build('imageName') inside script block [OK]
Common Mistakes:
  • Using key-value syntax incorrectly
  • Adding extra words inside the string
  • Missing quotes around image name
3. Given this Jenkins pipeline snippet inside a script block:
def image = docker.build('myapp:latest')
image.push()

What will happen when this pipeline runs?
medium
A. Builds the Docker image 'myapp:latest' and pushes it to the default Docker registry
B. Builds the Docker image but does not push it anywhere
C. Pushes an existing image 'myapp:latest' without building it
D. Fails because image.push() is not a valid method

Solution

  1. Step 1: Understand docker.build() and image.push()

    The docker.build() command builds the image and returns an object representing it. Calling image.push() pushes the built image to the Docker registry.
  2. Step 2: Confirm the sequence of actions

    The code builds 'myapp:latest' and then pushes it to the default Docker registry configured in Jenkins.
  3. Final Answer:

    Builds the Docker image 'myapp:latest' and pushes it to the default Docker registry -> Option A
  4. Quick Check:

    docker.build() + image.push() builds and pushes [OK]
Hint: docker.build() returns image object; call push() to upload [OK]
Common Mistakes:
  • Assuming push happens automatically without calling push()
  • Thinking push() is invalid method
  • Confusing build and push steps
4. You wrote this Jenkins pipeline snippet:
pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        docker.build('myapp')
      }
    }
  }
}

But the build fails with error: docker.build is not recognized. What is the most likely cause?
medium
A. The docker variable is not available outside a script block
B. The Dockerfile is missing in the workspace
C. The Jenkins agent does not have Docker installed
D. The image name 'myapp' is invalid

Solution

  1. Step 1: Check usage of docker.build()

    In declarative pipelines, docker.build() must be called inside a script block to access the Docker DSL.
  2. Step 2: Identify the error cause

    Calling docker.build() directly in steps without script causes the method to be unrecognized.
  3. Final Answer:

    The docker variable is not available outside a script block -> Option A
  4. Quick Check:

    Use script block for docker.build() [OK]
Hint: Always wrap docker.build() inside script block [OK]
Common Mistakes:
  • Ignoring need for script block
  • Assuming missing Dockerfile causes this error
  • Blaming Docker installation without checking syntax
5. You want to build a Docker image in Jenkins pipeline and tag it with the current Git commit hash. Which snippet correctly achieves this inside a script block?
hard
A. def commit = sh('git rev-parse --short HEAD') def image = docker.build('myapp:commit')
B. def commit = sh(script: 'git rev-parse --short HEAD', returnStdout: true).trim() def image = docker.build("myapp:${commit}")
C. def commit = git rev-parse --short HEAD def image = docker.build('myapp:${commit}')
D. def commit = sh(script: 'git rev-parse --short HEAD') def image = docker.build('myapp:commit')

Solution

  1. Step 1: Correctly capture Git commit hash in Jenkins

    Use sh with returnStdout: true and trim() to get the commit hash as a string.
  2. Step 2: Use the commit variable in docker.build()

    Use double quotes and string interpolation to tag the image with the commit hash.
  3. Final Answer:

    def commit = sh(script: 'git rev-parse --short HEAD', returnStdout: true).trim() def image = docker.build("myapp:${commit}") -> Option B
  4. Quick Check:

    Use sh with returnStdout and string interpolation [OK]
Hint: Use sh(..., returnStdout:true).trim() and "myapp:${commit}" [OK]
Common Mistakes:
  • Not using returnStdout to capture command output
  • Using single quotes preventing variable interpolation
  • Assigning shell command output incorrectly
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