Docker setup and agent option

Install docker

# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl gnupg
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg

# Add the repository to Apt sources:
echo \
  "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update

Install docker

sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

Lab 1: with single agent

pipeline {
agent {
docker { image 'node:18.18.0-alpine3.18' }
  }
stages {
 stage('Test') {
  steps {
   sh 'node --version'
     }
   }
  }
}

Docker Volumes to mount, which can be used for caching data on the agent between Pipeline runs. The following example will cache ~/.m2 between Pipeline runs utilizing the maven container, avoiding the need to re-download dependencies for subsequent Pipeline runs.

lab2:

pipeline {
agent {
  docker {
    image 'maven:3.9.3-eclipse-temurin-17'
    args '-v $HOME/.m2:/root/.m2'
   }
  }
stages {
  stage('Build') {
   steps {
    sh 'mvn -B'
    }
  }
 }
}

Combining Docker and Pipeline allows a Jenkinsfile to use multiple types of technologies, by combining the agent {} directive with different stages.

 

pipeline {
agent none
stages {
   stage('Back-end') {
     agent {
     docker { image 'maven:3.9.4-eclipse-temurin-17-alpine' }
      }
     steps {
       sh 'mvn --version'
     }
    }
   stage('Front-end') {
     agent {
       docker { image 'node:18.18.0-alpine3.18' }
      }
    steps {
     sh 'node --version'
    }
   }
 }
}