0
0
Jenkinsdevops~20 mins

Docker agent in Jenkinsfile - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Docker Agent Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of Jenkinsfile with Docker agent

What will be the output of the following Jenkinsfile snippet when run on a Jenkins server with Docker installed?

Jenkins
pipeline {
  agent {
    docker {
      image 'alpine:3.14'
      args '-v /tmp:/tmp'
    }
  }
  stages {
    stage('Test') {
      steps {
        sh 'echo Hello from Docker container'
      }
    }
  }
}
AJenkins pipeline skipped
BError: Docker image not found
CHello from Docker container
DPermission denied error
Attempts:
2 left
💡 Hint

Think about what the sh step inside a Docker agent does.

Configuration
intermediate
2:00remaining
Correct Docker agent syntax in Jenkinsfile

Which of the following Jenkinsfile snippets correctly defines a Docker agent with the image node:16 and mounts the workspace directory?

A
agent {
  docker {
    image 'node:16'
    args "-v ${WORKSPACE}:/workspace"
  }
}
B
agent {
  dockerfile {
    filename 'node:16'
  }
}
C
agent {
  docker {
    image 'node:16'
    args '-v /var/jenkins_home:/workspace'
  }
}
D
agent {
  docker {
    image 'node:16'
    args '-v $WORKSPACE:/workspace'
  }
}
Attempts:
2 left
💡 Hint

Remember to use the correct environment variable syntax for mounting volumes.

Troubleshoot
advanced
2:00remaining
Troubleshooting Docker agent failure in Jenkinsfile

A Jenkins pipeline using a Docker agent fails with the error: Cannot connect to the Docker daemon. What is the most likely cause?

AThe Docker image name is misspelled in the Jenkinsfile
BDocker daemon is not running on the Jenkins agent machine
CThe Jenkinsfile syntax is invalid
DThe Jenkins user does not have permission to read the Jenkinsfile
Attempts:
2 left
💡 Hint

Think about what is needed to run Docker commands on the Jenkins agent.

🔀 Workflow
advanced
2:00remaining
Jenkins pipeline with Docker agent and environment variables

Consider this Jenkinsfile snippet:

pipeline {
  agent {
    docker {
      image 'python:3.9'
      args '-e ENV=production'
    }
  }
  stages {
    stage('Print Env') {
      steps {
        sh 'echo $ENV'
      }
    }
  }
}

What will be printed when this pipeline runs?

AError: ENV variable not found
B$ENV
CAn empty line
Dproduction
Attempts:
2 left
💡 Hint

Check how environment variables passed via args are available inside the container.

Best Practice
expert
3:00remaining
Best practice for using Docker agent in Jenkinsfile for caching dependencies

You want to speed up your Jenkins pipeline builds by caching dependencies inside a Docker container used as an agent. Which approach is best?

AUse a Docker agent with a volume mount to cache dependencies between builds
BPull the Docker image fresh every build without caching
CUse a Docker agent without volume mounts and install dependencies every build
DRun the pipeline on the Jenkins master node without Docker
Attempts:
2 left
💡 Hint

Think about how Docker volumes can help keep data between container runs.