Bird
Raised Fist0
Jenkinsdevops~20 mins

Docker agent in Jenkinsfile - Practice Problems & Coding Challenges

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

Practice

(1/5)
1. What is the main purpose of using a docker agent in a Jenkinsfile?
easy
A. To speed up the Jenkins UI loading time
B. To run the build inside a container for a consistent environment
C. To deploy the application automatically after build
D. To backup Jenkins configuration files

Solution

  1. Step 1: Understand what a Docker agent does in Jenkins

    A Docker agent runs the build steps inside a Docker container, ensuring the environment is consistent and isolated.
  2. Step 2: Compare options with this purpose

    Only To run the build inside a container for a consistent environment describes running builds inside containers for consistency. Other options describe unrelated tasks.
  3. Final Answer:

    To run the build inside a container for a consistent environment -> Option B
  4. Quick Check:

    Docker agent purpose = consistent build environment [OK]
Hint: Docker agent means build runs inside container [OK]
Common Mistakes:
  • Confusing Docker agent with deployment step
  • Thinking Docker agent speeds up UI
  • Assuming Docker agent manages backups
2. Which of the following is the correct syntax to specify a Docker agent in a Jenkinsfile?
easy
A. agent { docker image 'node:14' }
B. agent docker image: 'node:14'
C. agent docker { 'node:14' }
D. agent { docker { image 'node:14' } }

Solution

  1. Step 1: Recall Jenkinsfile Docker agent syntax

    The correct syntax uses a block: agent { docker { image 'node:14' } }.
  2. Step 2: Check each option

    agent { docker { image 'node:14' } } matches the correct block syntax. Options A, B, and D have syntax errors or missing braces.
  3. Final Answer:

    agent { docker { image 'node:14' } } -> Option D
  4. Quick Check:

    Correct Docker agent syntax = agent { docker { image 'node:14' } } [OK]
Hint: Docker agent uses nested braces with image key [OK]
Common Mistakes:
  • Omitting braces around docker block
  • Using colon instead of braces
  • Placing image outside docker block
3. Given this Jenkinsfile snippet:
pipeline {
  agent {
    docker {
      image 'python:3.12'
      args '-v /tmp:/tmp'
    }
  }
  stages {
    stage('Test') {
      steps {
        sh 'python --version'
      }
    }
  }
}

What will be the output of the sh 'python --version' command?
medium
A. Python 3.12.x
B. Python 2.7.x
C. Command not found error
D. Python 3.8.x

Solution

  1. Step 1: Identify the Docker image used

    The image specified is 'python:3.12', which contains Python version 3.12.
  2. Step 2: Understand the command inside the container

    The command python --version runs inside the container, so it will output the Python version installed in that image, which is 3.12.x.
  3. Final Answer:

    Python 3.12.x -> Option A
  4. Quick Check:

    Docker image version matches command output [OK]
Hint: Output matches Docker image Python version [OK]
Common Mistakes:
  • Assuming host Python version is used
  • Confusing image tag with Python version
  • Expecting command not found error
4. You wrote this Jenkinsfile snippet:
pipeline {
  agent {
    docker {
      image 'ubuntu'
      args '-it'
    }
  }
  stages {
    stage('Build') {
      steps {
        sh 'echo Hello'
      }
    }
  }
}

But the build fails with error: docker: Error response from daemon: the input device is not a TTY. What is the best fix?
medium
A. Change image to ubuntu:latest
B. Add tty true to the docker agent block
C. Remove the -it argument from args
D. Replace sh with bat

Solution

  1. Step 1: Understand the error message

    The error says the input device is not a TTY, caused by using -it (interactive + tty) in a non-interactive Jenkins build.
  2. Step 2: Fix the Docker args

    Removing -it from args avoids requesting a TTY, fixing the error.
  3. Final Answer:

    Remove the -it argument from args -> Option C
  4. Quick Check:

    Remove interactive TTY args in Jenkins Docker agent [OK]
Hint: Avoid -it args in Jenkins Docker agents [OK]
Common Mistakes:
  • Adding tty true without removing -it
  • Changing image without fixing args
  • Using Windows bat step on Linux agent
5. You want to run a Jenkins pipeline using a Docker agent with the image maven:3.8.6-jdk-17 and mount the host directory /var/maven-repo to /root/.m2/repository inside the container. Which Jenkinsfile snippet correctly sets this up?
hard
A. agent { docker { image 'maven:3.8.6-jdk-17' args '-v /var/maven-repo:/root/.m2/repository' } }
B. agent { docker { image 'maven:3.8.6-jdk-17' args '-v /root/.m2/repository:/var/maven-repo' } }
C. agent { docker { image 'maven:3.8.6-jdk-17' args '-v /var/maven-repo' } }
D. agent { docker { image 'maven:3.8.6-jdk-17' args '--mount source=/var/maven-repo,target=/root/.m2/repository' } }

Solution

  1. Step 1: Understand Docker volume mount syntax

    The syntax to mount a host directory to a container path is -v host_path:container_path.
  2. Step 2: Match the mount paths correctly

    The host directory /var/maven-repo should be mounted to /root/.m2/repository inside the container, so the args must be -v /var/maven-repo:/root/.m2/repository.
  3. Step 3: Check options

    agent { docker { image 'maven:3.8.6-jdk-17' args '-v /var/maven-repo:/root/.m2/repository' } } matches the correct syntax and paths. agent { docker { image 'maven:3.8.6-jdk-17' args '-v /root/.m2/repository:/var/maven-repo' } } reverses paths. agent { docker { image 'maven:3.8.6-jdk-17' args '-v /var/maven-repo' } } misses container path. agent { docker { image 'maven:3.8.6-jdk-17' args '--mount source=/var/maven-repo,target=/root/.m2/repository' } } uses incorrect mount syntax for Docker CLI.
  4. Final Answer:

    agent { docker { image 'maven:3.8.6-jdk-17' args '-v /var/maven-repo:/root/.m2/repository' } } -> Option A
  5. Quick Check:

    Correct volume mount syntax = agent { docker { image 'maven:3.8.6-jdk-17' args '-v /var/maven-repo:/root/.m2/repository' } } [OK]
Hint: Use -v host:container for volume mounts in Docker args [OK]
Common Mistakes:
  • Reversing host and container paths
  • Omitting container path in volume mount
  • Using --mount instead of -v in args
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