Prerequisites: Install Docker on CentOS
Before using Docker to deploy Jenkins, ensure Docker is installed and running on your CentOS system. Run the following commands to install Docker:
sudo yum update -y
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
sudo yum install -y docker-ce docker-ce-cli containerd.io
sudo systemctl start docker
sudo systemctl enable docker
Verify the installation with docker --version
.
Step 1: Pull the Jenkins Docker Image
Fetch the official Jenkins Long-Term Support (LTS) image from Docker Hub (recommended for production stability):
docker pull jenkins/jenkins:lts
This command downloads the latest stable Jenkins image to your local machine.
Step 2: Run the Jenkins Container
Start a Jenkins container with port mappings and volume persistence. Use this command (adjust paths as needed):
docker run -d \
--name jenkins \
-p 8080:8080 \ # Map host port 8080 to container port 8080 (web interface)
-p 50000:50000 \ # Map host port 50000 to container port 50000 (agent communication)
-v jenkins_home:/var/jenkins_home \ # Persistent volume for Jenkins data (Docker-managed)
-v /var/run/docker.sock:/var/run/docker.sock \ # Allow Jenkins to control host Docker (for Docker-in-Docker)
jenkins/jenkins:lts
Key parameters:
-d
: Run the container in detached (background) mode.--name jenkins
: Assign a memorable name to the container.-v
): Ensure Jenkins configuration/data persists even if the container is deleted.Step 3: Access Jenkins Initial Setup
Open a browser and navigate to http://<your_server_ip>:8080
. You’ll see the Jenkins unlock page. Retrieve the initial admin password by running:
docker exec jenkins cat /var/jenkins_home/secrets/initialAdminPassword
Copy the displayed password and paste it into the unlock form. Proceed to install recommended plugins (e.g., Git, Pipeline, Docker) to enable core CI/CD functionality.
Step 4: Configure Jenkins for Docker Integration
To allow Jenkins to build/push Docker images or run containers, install the Docker Plugin:
unix:///var/run/docker.sock
(enables communication with the host Docker daemon).Step 5: Create a Sample Jenkins Pipeline
Create a new Pipeline job to automate building/pushing a Docker image. Example pipeline script (Jenkinsfile
) in your repository:
pipeline {
agent any
stages {
stage('Checkout') {
steps {
git url: 'https://github.com/your-repo/your-app.git', branch: 'main' // Replace with your repo
}
}
stage('Build Docker Image') {
steps {
script {
def imageName = "your-dockerhub-username/your-app:${env.BUILD_ID}"
docker.build(imageName) // Build image with BUILD_ID as tag
}
}
}
stage('Push to Docker Hub') {
steps {
script {
def imageName = "your-dockerhub-username/your-app:${env.BUILD_ID}"
docker.withRegistry('https://registry.hub.docker.com', 'docker-hub-credentials') { // Use credentials ID from step 4
docker.image(imageName).push() // Push image to Docker Hub
}
}
}
}
}
}
This pipeline checks out code, builds a Docker image, and pushes it to Docker Hub. Replace placeholders (e.g., your-repo
, your-dockerhub-username
) with your actual values.
Optional: Use Docker Compose for Easier Management
For simplified container orchestration, create a docker-compose.yml
file:
version: '3'
services:
jenkins:
image: jenkins/jenkins:lts
container_name: jenkins
ports:
- "8080:8080"
- "50000:50000"
volumes:
- jenkins_home:/var/jenkins_home
- /var/run/docker.sock:/var/run/docker.sock
volumes:
jenkins_home:
Start the container with docker-compose up -d
and manage it using docker-compose
commands (e.g., docker-compose stop
, docker-compose logs
).