- Install Docker on the Jenkins Server
If Docker is not installed yet, install it first.
Then, add Jenkins to the Docker group to allow it to run Docker commands without sudo
:
sudo groupadd docker
sudo usermod -aG docker jenkins
sudo systemctl restart jenkins
- Install the Docker Plugin in Jenkins
Go to Jenkins → Manage Jenkins → Manage Plugins
Search for “Docker Plugin” (by CloudBees)
Install and restart Jenkins
- Configure Docker in Jenkins
Now, configure Jenkins to use Docker as an agent:
Go to: Jenkins → Manage Jenkins → Nodes
Click “Clouds” → “New cloud”
Select “Docker”
Set Docker Host URI to:
unix:///var/run/docker.sock
Click Test Connection (It should show the Docker version)
- Add a Docker Agent Template
Click “Add Docker Template”
Choose an image like jenkins/inbound-agent
Set:
- Labels:
docker-agent
- Remote File System Root:
/home/jenkins
- Usage: “Only build jobs with label expressions matching this node”
Click Save
- Run a Jenkins Job in Docker
Use the Docker Pipeline Plugin to run jobs in a container:
pipeline {
agent { label 'docker-agent' }
stages {
stage('Hello') {
steps {
echo 'Hello World'
}
}
}
}
Now, Jenkins will spin up a Docker container to run the job!