Prerequisites
Before using Docker containers with Jenkins on Ubuntu, ensure Docker is installed and running. You can install Docker using the official repository:
sudo apt update sudo apt install apt-transport-https ca-certificates curl gnupg-agent software-properties-common curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" sudo apt update sudo apt install docker-ce docker-ce-cli containerd.io sudo systemctl start docker sudo systemctl enable docker
Verify Docker is running with docker --version
and docker run hello-world
.
1. Deploy Jenkins Using Docker
To quickly set up Jenkins, pull the official jenkins/jenkins:lts
(Long-Term Support) image and run it as a container. The key flags are:
-d
: Run in detached mode (background).-p 8080:8080
: Map the container’s Jenkins web interface (port 8080) to the host’s port 8080.-p 50000:50000
: Expose the Jenkins agent port (for distributed builds).-v jenkins_home:/var/jenkins_home
: Use a Docker volume to persist Jenkins data (configurations, jobs, logs) across container restarts.Command:
docker run -d -p 8080:8080 -p 50000:50000 --name jenkins -v jenkins_home:/var/jenkins_home jenkins/jenkins:lts
If you prefer local directory persistence (e.g., /opt/jenkins_home
), replace the volume flag with -v /opt/jenkins_home:/var/jenkins_home
.
2. Access and Initial Setup
docker exec jenkins cat /var/jenkins_home/secrets/initialAdminPassword
Copy the password and paste it into the Jenkins web interface (http://3. Configure Jenkins to Use Docker
To allow Jenkins to control Docker (e.g., build/push images, run containers), grant it access to the Docker daemon:
jenkins
) execute Docker commands without sudo
.sudo usermod -aG docker jenkins sudo systemctl restart jenkins
docker exec -it jenkins bash docker ps # Should list running containers without errors
This setup enables Jenkins to interact with Docker for tasks like building images from pipelines.
4. Create a Jenkins Pipeline with Docker
Use a Jenkinsfile
to define a pipeline that leverages Docker for building/testing applications. Below is an example for a Node.js app:
pipeline { agent any environment { DOCKER_IMAGE = "my-node-app:${env.BUILD_ID}" // Dynamic image name with build ID } stages { stage('Checkout') { steps { checkout scm // Pull code from Git } } stage('Build') { steps { sh 'npm install' // Install dependencies } } stage('Test') { steps { sh 'npm test' // Run unit tests } } stage('Build Docker Image') { steps { script { docker.build(DOCKER_IMAGE) // Build image using Dockerfile in project root } } } stage('Run Container') { steps { script { docker.image(DOCKER_IMAGE).withRun('-p 3000:3000') { // Run container on host port 3000 sh 'curl http://localhost:3000' // Verify app is running } } } } } post { always { cleanWs() // Clean up workspace after build } } }
agent any
: Uses any available Jenkins agent.docker.build()
: Constructs a Docker image from the Dockerfile
in the project.withRun()
: Runs the container and executes commands inside it (e.g., curl
to test the app).main
branch).5. Optional: Use Docker-in-Docker (DinD)
For advanced scenarios (e.g., isolating Docker operations within the Jenkins container), use DinD. This involves:
/var/run/docker.sock
) into the Jenkins container.Example docker run
command for DinD:
docker run --name jenkins -u root -d -p 8080:8080 -p 50000:50000 \ -v /var/jenkins_home:/var/jenkins_home \ -v /var/run/docker.sock:/var/run/docker.sock \ -v /usr/bin/docker:/usr/bin/docker \ jenkins/jenkins:lts
Note: DinD can introduce security risks (e.g., container breakout) and performance overhead. Use it only if necessary, and ensure proper isolation (e.g., use a dedicated Docker daemon for Jenkins).
Troubleshooting Tips
docker logs jenkins
for errors (e.g., port conflicts, disk space).jenkins_home
volume is correctly mounted (verify with docker volume inspect jenkins_home
).