Prerequisites
Before using Jenkins with Docker on CentOS, ensure Docker is installed and running. If not, follow these steps:
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.Install Jenkins via Docker
You can install Jenkins in two ways: using the official Jenkins image or a custom CentOS-based image.
The official jenkins/jenkins:lts image (LTS = Long-Term Support) is the most stable choice.
docker pull jenkins/jenkins:lts to download the latest LTS version.docker run -d \
--name jenkins \
-p 8080:8080 \ # Map host port 8080 to container's Jenkins web UI
-p 50000:50000 \ # Map host port 50000 for Jenkins agent communication
-v jenkins_home:/var/jenkins_home \ # Persistent volume for Jenkins data
--restart=on-failure \ # Auto-restart container if it fails
jenkins/jenkins:lts
Key flags:
-d: Run in detached mode (background).-v jenkins_home:/var/jenkins_home: Mount a Docker volume to persist Jenkins configurations, jobs, and plugins.--restart=on-failure: Ensures the container restarts automatically if it crashes.If you need a CentOS-specific environment (e.g., for compatibility with existing CentOS-based tools), create a custom image using a Dockerfile:
mkdir custom-jenkins && cd custom-jenkins.Dockerfile with the following content:FROM centos:7
RUN yum install -y java-1.8.0-openjdk-devel git wget && yum clean all
RUN wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.repo && \
rpm --import https://pkg.jenkins.io/redhat/jenkins.io.key && \
yum install -y jenkins && yum clean all
ENV JENKINS_HOME /var/lib/jenkins
EXPOSE 8080
CMD ["/usr/bin/java", "-Djava.awt.headless=true", "-jar", "/usr/lib/jenkins/jenkins.war", "--webroot=/var/cache/jenkins/war", "--httpPort=8080"]
This installs Java (required for Jenkins), Git (for source control), and Jenkins on CentOS 7.docker build -t custom-jenkins . to create the image.docker run -d -p 8080:8080 --name jenkins custom-jenkins to start the container.Initialize Jenkins
After starting the container, complete the initial setup:
docker logs jenkins to view the logs, then copy the auto-generated password (e.g., 447a868d1fd64cbc8dbf66792f31425d).http://<your_server_ip>:8080. Paste the password into the “Unlock Jenkins” page.Integrate Jenkins with Docker
To use Docker within Jenkins (e.g., building/publishing Docker images), configure the Docker plugin and connect to the Docker daemon:
unix:///var/run/docker.sock (default for local Docker).pipeline {
agent any
stages {
stage('Build Docker Image') {
steps {
script {
def imageName = "my-app:${env.BUILD_ID}"
docker.build(imageName)
echo "Built image: ${imageName}"
}
}
}
}
}
Key Tips for Production Use
latest (e.g., jenkins/jenkins:lts instead of jenkins/jenkins:latest) for stability.jenkins_home volume (e.g., docker run --rm -v jenkins_home:/volume -v $(pwd):/backup ubuntu tar cvf /backup/jenkins_backup.tar /volume).--cpus="2" --memory="4g" in the docker run command) to prevent resource exhaustion.