docker Agent Directives

The docker and dockerfile Agent Directives

Declarative Pipeline offers the same machinery as two agent types: docker runs a stage in a container from an image, and dockerfile first builds one from a Dockerfile in the workspace. reuseNode true keeps the stage on the enclosing node and workspace, so the checkout is not repeated:

dk-decl: a docker stage and a dockerfile stageGroovy
pipeline {
  agent { label 'docker' }
  stages {
    stage('Checkout') {
      steps { git url: 'https://github.com/binarybehemoth/booknest-jenkins.git', branch: 'main' }
    }
    stage('Lint on Node 22') {
      agent {
        docker { image 'node:22-slim'; args '--cpus 1 --memory 1g'; reuseNode true }
      }
      steps { sh 'node --version && node --check app.js && echo "app.js parses on Node 22"' }
    }
    stage('Dependencies image') {
      agent {
        dockerfile { additionalBuildArgs '--target deps'; reuseNode true }
      }
      steps { sh 'node --version && ls /app/node_modules | wc -l' }
    }
  }
}
Output
app.js parses on Node 22
...
79

The dockerfile agent built Docker's Dockerfile up to its deps stage (with BuildKit 10,294 ) and ran the steps beside /app/node_modules. Its image, named by a hash (576f6b26..., 339 MB), stays on the agent, so schedule docker image prune --all --filter until=24h there.