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.- Volume mounts (
-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://. 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:
- Go to Manage Jenkins > Manage Plugins > Available.
- Search for “Docker Plugin” and click Install without restart.
- Configure the Docker cloud: Navigate to Manage Jenkins > Configure System > Cloud > Add a new cloud > Docker.
- Set the Docker Host URI to
unix:///var/run/docker.sock(enables communication with the host Docker daemon). - Add credentials for Docker registries (e.g., Docker Hub) if pushing images.
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).
以上就是关于“CentOS Jenkins如何使用Docker容器”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm