温馨提示×

centos jenkins如何使用Docker

小樊
45
2025-11-10 04:03:21
栏目: 智能运维

Prerequisites
Before using Jenkins with Docker on CentOS, ensure Docker is installed and running. If not, follow these steps:

  1. Update system packages: sudo yum update -y.
  2. Install Docker dependencies: sudo yum install -y yum-utils device-mapper-persistent-data lvm2.
  3. Add Docker’s official repository: sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo.
  4. Install Docker CE: sudo yum install -y docker-ce docker-ce-cli containerd.io.
  5. 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.

  1. Pull the Jenkins Image:
    Run docker pull jenkins/jenkins:lts to download the latest LTS version.
  2. Run the Jenkins Container:
    Use the following command to start a container with persistent storage (to retain data after restarts) and port mappings:
    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.

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:

  1. Create a Directory for the Dockerfile:
    mkdir custom-jenkins && cd custom-jenkins.
  2. Write the Dockerfile:
    Create a file named 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.
  3. Build the Image:
    Run docker build -t custom-jenkins . to create the image.
  4. Run the Container:
    Use docker run -d -p 8080:8080 --name jenkins custom-jenkins to start the container.

Initialize Jenkins
After starting the container, complete the initial setup:

  1. Get the Initial Admin Password:
    Run docker logs jenkins to view the logs, then copy the auto-generated password (e.g., 447a868d1fd64cbc8dbf66792f31425d).
  2. Access the Jenkins Web UI:
    Open a browser and navigate to http://<your_server_ip>:8080. Paste the password into the “Unlock Jenkins” page.
  3. Install Recommended Plugins:
    Select “Install suggested plugins” to install essential plugins (e.g., Git, Pipeline, SSH). This may take a few minutes.
  4. Create an Admin User:
    Fill in the user details (username, password, full name, email) and click “Save and Finish”.
  5. 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:

  1. Install Docker Plugin:
    Go to “Manage Jenkins” > “Manage Plugins” > “Available”. Search for “Docker Pipeline” and install it.
  2. 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”).
  3. 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:lts instead of jenkins/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_home volume (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 the docker run command) to prevent resource exhaustion.

0