centos jenkins如何使用Docker
Prerequisites
Before using Jenkins with Docker on CentOS, ensure Docker is installed and running. If not, follow these steps:
- Update system packages:
sudo yum update -y. - Install Docker dependencies:
sudo yum install -y yum-utils device-mapper-persistent-data lvm2. - Add Docker’s official repository:
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo. - Install Docker CE:
sudo yum install -y docker-ce docker-ce-cli containerd.io. - Start and enable Docker:
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.
Option 1: Use Official Jenkins Image (Recommended)
The official jenkins/jenkins:lts image (LTS = Long-Term Support) is the most stable choice.
- Pull the Jenkins Image:
Rundocker pull jenkins/jenkins:ltsto download the latest LTS version. - Run the Jenkins Container:
Use the following command to start a container with persistent storage (to retain data after restarts) and port mappings:Key flags: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-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.
Option 2: Create a Custom CentOS-Based Jenkins Image
If you need a CentOS-specific environment (e.g., for compatibility with existing CentOS-based tools), create a custom image using a Dockerfile:
- Create a Directory for the Dockerfile:
mkdir custom-jenkins & & cd custom-jenkins. - Write the Dockerfile:
Create a file namedDockerfilewith the following content:This installs Java (required for Jenkins), Git (for source control), and Jenkins on CentOS 7.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"] - Build the Image:
Rundocker build -t custom-jenkins .to create the image. - Run the Container:
Usedocker run -d -p 8080:8080 --name jenkins custom-jenkinsto start the container.
Initialize Jenkins
After starting the container, complete the initial setup:
- Get the Initial Admin Password:
Rundocker logs jenkinsto view the logs, then copy the auto-generated password (e.g.,447a868d1fd64cbc8dbf66792f31425d). - Access the Jenkins Web UI:
Open a browser and navigate tohttp://< your_server_ip> :8080. Paste the password into the “Unlock Jenkins” page. - Install Recommended Plugins:
Select “Install suggested plugins” to install essential plugins (e.g., Git, Pipeline, SSH). This may take a few minutes. - Create an Admin User:
Fill in the user details (username, password, full name, email) and click “Save and Finish”. - Start Using Jenkins:
Click “Start using Jenkins” to access the dashboard.
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:
- Install Docker Plugin:
Go to “Manage Jenkins” > “Manage Plugins” > “Available”. Search for “Docker Pipeline” and install it. - Configure Docker Cloud:
Navigate to “Manage Jenkins” > “Configure System”. Under the “Cloud” section, click “Add a new cloud” > “Docker”.- Enter a name (e.g., “Local Docker”).
- Set “Docker Host URI” to
unix:///var/run/docker.sock(default for local Docker). - Click “Test Connection” to verify access (should return “Connection successful”).
- Create a Pipeline Job:
- Go to “New Item” > enter a job name (e.g., “Docker Build”) > select “Pipeline” > click “OK”.
- In the pipeline script, add a stage to build a Docker image:
pipeline { agent any stages { stage('Build Docker Image') { steps { script { def imageName = "my-app:${ env.BUILD_ID} " docker.build(imageName) echo "Built image: ${ imageName} " } } } } } - Save and run the job. The pipeline will build a Docker image tagged with the build ID.
Key Tips for Production Use
- Use Specific Image Tags: Avoid
latest(e.g.,jenkins/jenkins:ltsinstead ofjenkins/jenkins:latest) for stability. - Secure Jenkins: Change the default admin password, restrict access via firewall (e.g., allow only internal IPs to port 8080), and consider enabling HTTPS.
- Backup Data: Regularly back up the
jenkins_homevolume (e.g.,docker run --rm -v jenkins_home:/volume -v $(pwd):/backup ubuntu tar cvf /backup/jenkins_backup.tar /volume). - Resource Limits: Allocate CPU/memory limits to the Jenkins container (e.g.,
--cpus="2" --memory="4g"in thedocker runcommand) to prevent resource exhaustion.
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: centos jenkins如何使用Docker
本文地址: https://pptw.com/jishu/745993.html
