温馨提示×

Ubuntu Jenkins如何使用Docker容器

小樊
50
2025-09-18 19:36:06
栏目: 智能运维

Prerequisites
Before using Docker containers with Jenkins on Ubuntu, ensure Docker is installed and running. You can install Docker using the official repository:

sudo apt update sudo apt install apt-transport-https ca-certificates curl gnupg-agent software-properties-common curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" sudo apt update sudo apt install docker-ce docker-ce-cli containerd.io sudo systemctl start docker sudo systemctl enable docker 

Verify Docker is running with docker --version and docker run hello-world.

1. Deploy Jenkins Using Docker
To quickly set up Jenkins, pull the official jenkins/jenkins:lts (Long-Term Support) image and run it as a container. The key flags are:

  • -d: Run in detached mode (background).
  • -p 8080:8080: Map the container’s Jenkins web interface (port 8080) to the host’s port 8080.
  • -p 50000:50000: Expose the Jenkins agent port (for distributed builds).
  • -v jenkins_home:/var/jenkins_home: Use a Docker volume to persist Jenkins data (configurations, jobs, logs) across container restarts.

Command:

docker run -d -p 8080:8080 -p 50000:50000 --name jenkins -v jenkins_home:/var/jenkins_home jenkins/jenkins:lts 

If you prefer local directory persistence (e.g., /opt/jenkins_home), replace the volume flag with -v /opt/jenkins_home:/var/jenkins_home.

2. Access and Initial Setup

  • Unlock Jenkins: After starting the container, retrieve the initial admin password from the container logs:
    docker exec jenkins cat /var/jenkins_home/secrets/initialAdminPassword 
    Copy the password and paste it into the Jenkins web interface (http://:8080).
  • Install Plugins: During setup, choose “Install suggested plugins” to get essential tools (e.g., Git, Pipeline). Alternatively, manually install plugins like Docker Pipeline (for integrating Docker in pipelines) via Manage Jenkins > Manage Plugins.

3. Configure Jenkins to Use Docker
To allow Jenkins to control Docker (e.g., build/push images, run containers), grant it access to the Docker daemon:

  • Add Jenkins User to Docker Group: This lets the Jenkins process (running as jenkins) execute Docker commands without sudo.
    sudo usermod -aG docker jenkins sudo systemctl restart jenkins 
  • Verify Permissions: Log in to the Jenkins container and test Docker connectivity:
    docker exec -it jenkins bash docker ps # Should list running containers without errors 

This setup enables Jenkins to interact with Docker for tasks like building images from pipelines.

4. Create a Jenkins Pipeline with Docker
Use a Jenkinsfile to define a pipeline that leverages Docker for building/testing applications. Below is an example for a Node.js app:

  • Jenkinsfile:
    pipeline { agent any environment { DOCKER_IMAGE = "my-node-app:${env.BUILD_ID}" // Dynamic image name with build ID } stages { stage('Checkout') { steps { checkout scm // Pull code from Git } } stage('Build') { steps { sh 'npm install' // Install dependencies } } stage('Test') { steps { sh 'npm test' // Run unit tests } } stage('Build Docker Image') { steps { script { docker.build(DOCKER_IMAGE) // Build image using Dockerfile in project root } } } stage('Run Container') { steps { script { docker.image(DOCKER_IMAGE).withRun('-p 3000:3000') { // Run container on host port 3000 sh 'curl http://localhost:3000' // Verify app is running } } } } } post { always { cleanWs() // Clean up workspace after build } } } 
  • Key Steps Explained:
    • agent any: Uses any available Jenkins agent.
    • docker.build(): Constructs a Docker image from the Dockerfile in the project.
    • withRun(): Runs the container and executes commands inside it (e.g., curl to test the app).
  • Trigger Builds: Configure your Git repository in the Jenkins job settings to trigger the pipeline on code changes (e.g., push to main branch).

5. Optional: Use Docker-in-Docker (DinD)
For advanced scenarios (e.g., isolating Docker operations within the Jenkins container), use DinD. This involves:

  • Mounting the Docker socket (/var/run/docker.sock) into the Jenkins container.
  • Installing Docker CLI inside the container.

Example docker run command for DinD:

docker run --name jenkins -u root -d -p 8080:8080 -p 50000:50000 \ -v /var/jenkins_home:/var/jenkins_home \ -v /var/run/docker.sock:/var/run/docker.sock \ -v /usr/bin/docker:/usr/bin/docker \ jenkins/jenkins:lts 

Note: DinD can introduce security risks (e.g., container breakout) and performance overhead. Use it only if necessary, and ensure proper isolation (e.g., use a dedicated Docker daemon for Jenkins).

Troubleshooting Tips

  • Container Not Starting: Check logs with docker logs jenkins for errors (e.g., port conflicts, disk space).
  • Data Loss: Ensure the jenkins_home volume is correctly mounted (verify with docker volume inspect jenkins_home).
  • Plugin Installation Failures: Switch to a mirror update site (e.g., Tsinghua University’s mirror) in Manage Jenkins > Manage Plugins > Advanced to avoid network issues.

0