Bird
Raised Fist0
Jenkinsdevops~20 mins

Docker agents for isolation in Jenkins - 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 Jenkins pipeline with Docker agent
What will be the output of this Jenkins pipeline snippet when run on a Jenkins master with Docker installed?
Jenkins
pipeline {
  agent {
    docker {
      image 'alpine:3.14'
      args '-v /tmp:/tmp'
    }
  }
  stages {
    stage('Test') {
      steps {
        sh 'echo Hello from inside Docker'
      }
    }
  }
}
AHello from inside Docker
BError: Docker daemon not found
CPermission denied when accessing /tmp
DPipeline skipped due to missing image
Attempts:
2 left
💡 Hint
The pipeline uses a Docker agent with Alpine image and runs a simple echo command inside the container.
❓ Configuration
intermediate
2:00remaining
Correct Docker agent syntax in Jenkinsfile
Which Jenkinsfile snippet correctly defines a Docker agent that uses the 'node:18' image and mounts the host directory '/data' to '/app/data' inside the container?
A
agent {
  docker {
    image 'node:18'
    args '-v /data:/app/data'
  }
}
B
agent {
  docker {
    image 'node:18'
    args '--mount /data:/app/data'
  }
}
C
agent {
  docker {
    image 'node:18'
    args '--volume /data:/app/data'
  }
}
D
agent {
  docker {
    image 'node:18'
    args '-m /data:/app/data'
  }
}
Attempts:
2 left
💡 Hint
Docker volume mount syntax uses '-v' flag.
❓ Troubleshoot
advanced
2:00remaining
Troubleshooting Docker agent permission error
A Jenkins pipeline using a Docker agent fails with the error: 'permission denied while trying to connect to the Docker daemon socket'. What is the most likely cause?
AThe Docker image specified does not exist in the registry.
BThe Jenkins user does not have permission to access the Docker socket on the host.
CThe Jenkinsfile syntax for the Docker agent is incorrect.
DThe Docker daemon is running but out of disk space.
Attempts:
2 left
💡 Hint
Accessing Docker requires permission to the Docker socket file.
🔀 Workflow
advanced
2:00remaining
Jenkins pipeline with multiple Docker agents
In a Jenkins pipeline, you want to run one stage inside a Python Docker container and another stage inside a Node.js Docker container. Which pipeline structure correctly achieves this?
A
pipeline {
  agent { docker { image 'node:18' } }
  stages {
    stage('Python Stage') {
      agent { docker { image 'python:3.10' } }
      steps { sh 'python --version' }
    }
    stage('Node Stage') {
      steps { sh 'node --version' }
    }
  }
}
B
pipeline {
  agent { docker { image 'python:3.10' } }
  stages {
    stage('Python Stage') {
      steps { sh 'python --version' }
    }
    stage('Node Stage') {
      agent { docker { image 'node:18' } }
      steps { sh 'node --version' }
    }
  }
}
C
pipeline {
  agent none
  stages {
    stage('Python Stage') {
      agent { docker { image 'python:3.10' } }
      steps { sh 'python --version' }
    }
    stage('Node Stage') {
      agent { docker { image 'node:18' } }
      steps { sh 'node --version' }
    }
  }
}
D
pipeline {
  agent none
  stages {
    stage('Python Stage') {
      steps { sh 'docker run python:3.10 python --version' }
    }
    stage('Node Stage') {
      steps { sh 'docker run node:18 node --version' }
    }
  }
}
Attempts:
2 left
💡 Hint
Use 'agent none' at top level to specify agents per stage.
✅ Best Practice
expert
2:00remaining
Best practice for Docker agent cleanup in Jenkins
Which approach ensures Docker containers used as Jenkins agents are properly cleaned up after pipeline execution to avoid resource leaks?
AManually run 'docker rm -f' commands in pipeline steps after each stage.
BConfigure Jenkins to use 'docker' agents with 'alwaysPull true' to refresh images each run.
CUse 'agent any' and run Docker containers outside Jenkins to avoid cleanup issues.
DUse the 'docker' agent with 'reuseNode false' and rely on Jenkins to remove containers automatically.
Attempts:
2 left
💡 Hint
Jenkins Docker plugin can manage container lifecycle automatically.

Practice

(1/5)
1. What is the main benefit of using Docker agents in Jenkins for running jobs?
easy
A. They provide isolated environments to avoid conflicts between builds.
B. They speed up the Jenkins UI loading time.
C. They allow Jenkins to run without any plugins installed.
D. They automatically backup Jenkins configuration files.

Solution

  1. Step 1: Understand Docker agent purpose

    Docker agents run Jenkins jobs inside containers, isolating each build environment.
  2. Step 2: Identify the benefit of isolation

    This isolation prevents conflicts and keeps builds clean and reproducible.
  3. Final Answer:

    They provide isolated environments to avoid conflicts between builds. -> Option A
  4. Quick Check:

    Docker agents = isolated environments [OK]
Hint: Docker agents isolate builds to avoid conflicts [OK]
Common Mistakes:
  • Thinking Docker agents speed up Jenkins UI
  • Believing Docker agents remove need for plugins
  • Assuming Docker agents backup Jenkins configs
2. Which of the following is the correct syntax to specify a Docker agent with image python:3.12 in a Jenkins pipeline?
easy
A. agent { docker { image 'python:3.12' } }
B. agent { docker.image('python:3.12') }
C. agent { docker('python:3.12') }
D. agent { docker { image: 'python:3.12' } }

Solution

  1. Step 1: Recall Jenkins pipeline Docker agent syntax

    The correct syntax uses agent { docker { image 'image_name' } } format.
  2. Step 2: Match the syntax with options

    agent { docker { image 'python:3.12' } } matches the correct syntax exactly with single quotes and no colon.
  3. Final Answer:

    agent { docker { image 'python:3.12' } } -> Option A
  4. Quick Check:

    Correct Docker agent syntax = agent { docker { image 'python:3.12' } } [OK]
Hint: Use agent { docker { image 'name' } } syntax [OK]
Common Mistakes:
  • Using dot notation instead of block syntax
  • Adding colon inside image declaration
  • Using parentheses incorrectly
3. Given this Jenkins pipeline snippet:
pipeline {
  agent {
    docker {
      image 'node:18'
      args '-p 3000:3000'
    }
  }
  stages {
    stage('Run') {
      steps {
        sh 'node -v'
      }
    }
  }
}

What will be the output of the sh 'node -v' command?
medium
A. No output, command skipped
B. Error: node command not found
C. v12.x.x (Node.js version 12 output)
D. v18.x.x (Node.js version 18 output)

Solution

  1. Step 1: Identify Docker image used

    The pipeline uses the Docker image node:18, which includes Node.js version 18.
  2. Step 2: Understand command execution inside container

    The sh 'node -v' runs inside the container, so it will output Node.js version 18.
  3. Final Answer:

    v18.x.x (Node.js version 18 output) -> Option D
  4. Quick Check:

    Docker image node:18 runs Node.js v18 [OK]
Hint: Command runs inside specified Docker image environment [OK]
Common Mistakes:
  • Assuming host Node.js version runs instead
  • Expecting error due to missing node
  • Confusing image tag with different Node version
4. You wrote this Jenkins pipeline snippet to use a Docker agent:
pipeline {
  agent {
    docker {
      image 'ubuntu:latest'
      args '-v /var/run/docker.sock:/var/run/docker.sock'
    }
  }
  stages {
    stage('Test') {
      steps {
        sh 'docker ps'
      }
    }
  }
}

But the build fails with docker: command not found. What is the likely cause?
medium
A. The Jenkins agent cannot mount volumes.
B. The ubuntu:latest image does not have Docker installed.
C. The args option is incorrect syntax.
D. The docker ps command is invalid inside containers.

Solution

  1. Step 1: Check the Docker image capabilities

    The ubuntu:latest image is a minimal OS image and does not include Docker by default.
  2. Step 2: Understand why command fails

    Since Docker is not installed inside the container, docker ps command is not found, causing the error.
  3. Final Answer:

    The ubuntu:latest image does not have Docker installed. -> Option B
  4. Quick Check:

    Missing Docker in image = command not found error [OK]
Hint: Check if Docker is installed inside the container image [OK]
Common Mistakes:
  • Blaming volume mount syntax
  • Assuming docker commands always work in containers
  • Thinking docker ps is invalid inside containers
5. You want to run a Jenkins job inside a Docker container with a custom environment variable MY_VAR=hello and mount the workspace directory to /app inside the container. Which Jenkins pipeline snippet correctly sets this up?
hard
A. agent { docker { image 'python:3.12' args '-e MY_VAR=hello -v /app:$WORKSPACE' } }
B. agent { docker { image 'python:3.12' args '-v $WORKSPACE:/app -e MY_VAR=hello' } }
C. agent { docker { image 'python:3.12' args '-e MY_VAR=hello -v $WORKSPACE:/app' } }
D. agent { docker { image 'python:3.12' args '-v /app -e MY_VAR=hello' } }

Solution

  1. Step 1: Understand Docker args for environment and volume

    To set environment variables, use -e VAR=value. To mount volumes, use -v host_path:container_path.
  2. Step 2: Match correct volume and env var syntax

    agent { docker { image 'python:3.12' args '-e MY_VAR=hello -v $WORKSPACE:/app' } } correctly uses -e MY_VAR=hello and mounts $WORKSPACE to /app inside container.
  3. Final Answer:

    agent { docker { image 'python:3.12' args '-e MY_VAR=hello -v $WORKSPACE:/app' } } -> Option C
  4. Quick Check:

    Env var and volume mount syntax = agent { docker { image 'python:3.12' args '-e MY_VAR=hello -v $WORKSPACE:/app' } } [OK]
Hint: Use '-e VAR=value' and '-v host:container' in args [OK]
Common Mistakes:
  • Reversing volume mount paths
  • Omitting environment variable flag
  • Incorrect order or missing colon in volume
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