How to integrate Jenkins with Docker?
· Category: DevOps & CI/CD
Short answer
Integrate Jenkins with Docker by running builds inside Docker containers or using Docker agents. This ensures a clean, consistent environment for every build.
Steps
- Install the Docker Pipeline plugin.
- Define a Docker agent in the Jenkinsfile.
- Mount the Docker socket for Docker-in-Docker if needed.
Example
pipeline {
agent {
docker {
image 'node:18'
args '-v /var/run/docker.sock:/var/run/docker.sock'
}
}
stages {
stage('Build') {
steps {
sh 'npm ci'
sh 'docker build -t myapp .'
}
}
}
}
Tips
- Use specific image tags for reproducibility.
- Cache layers with BuildKit in CI.
- Clean up containers after builds.
Common issues
- Docker socket access is a security risk.
- Container permissions may mismatch the host.
- Large images slow down pipeline startup.