Docker Integration
Docker is widely used to build, test, and run applications. Jenkins can use Docker to provide clean, ephemeral build environments and to build/push container images.
Installing Docker Plugin
Install the "Docker" and "Docker Pipeline" plugins. Then, configure Jenkins to know about the Docker daemon: "Manage Jenkins" → "Configure System" → "Cloud" → "Docker".
Using Docker as Pipeline Agent
You can run your entire pipeline inside a Docker container. This ensures a reproducible environment.
pipeline {
agent {
docker {
image 'node:18-alpine'
args '-v /tmp:/tmp'
}
}
stages {
stage('Build') {
steps {
sh 'npm install'
sh 'npm run build'
}
}
}
}Building and Pushing Docker Images
Use the
docker DSL inside a script block.stage('Build Image') {
steps {
script {
docker.build("myapp:${env.BUILD_NUMBER}", ".")
docker.withRegistry('https://index.docker.io/v1/', 'docker-hub-credentials') {
docker.image("myapp:${env.BUILD_NUMBER}").push()
}
}
}
}Using Dockerfile in Pipeline
You can build an image directly from a Dockerfile in your repository:
docker.build("myapp", "-f Dockerfile .")Running Containers for Testing
You can run a container (e.g., a database) as a sidecar during tests.
docker.image('postgres:13').withRun('-e POSTGRES_PASSWORD=secret') { c ->
sh 'run_tests_that_connect_to_postgres'
}Two Minute Drill
- Use Docker as pipeline agent for clean, reproducible builds.
- Build and push images using
docker.build()and.push(). - Run temporary containers (e.g., databases) for integration tests.
- Always use credentials for registry access.
Need more clarification?
Drop us an email at career@quipoinfotech.com
