简体   繁体   English

在 Jenkins Docker 从站内运行 docker 构建

[英]Run docker build inside Jenkins Docker Slave

Currently I've a CI pipeline with the following stages:目前我有一个具有以下阶段的 CI 管道:

  1. Build建造
  2. Unit Tests单元测试
  3. Static Code Analysis Static代码分析

This is how my Jenkinsfile looks like:这就是我的 Jenkinsfile 的样子:

pipeline {
  agent any

  stages {
    stage("Install") {
      steps {
        sh "npm install"
      }
    }
    stage("Build") {
      steps {
        sh "npm run build"
      }
    }
    stage("Format") {
        steps {
            sh "npm run format"
        }
    }
    stage("Lint") {
      steps {
        sh "npm run lint"
      }
    }
    stage("Test") {
      steps {
        sh "npm run test"
      }
    }
    stage("Code Coverage") {
      steps {
        sh "npm run test:cov"
        publishHTML(target: [
            reportDir: "./coverage/lcov-report",
            reportFiles: "index.html",
            reportName: "Jest Coverage Report"
        ])
      }
    }
    stage("End-To-End Testing") {
      steps {
        sh "npm run test:e2e"
      }
    }
  }
}

I want to add more stages to my pipeline:我想在我的管道中添加更多阶段:

  1. Build and tag Docker Image from Dockerfile构建并标记 Docker 图像来自 Dockerfile
  2. Push the image to the Docker Hub将镜像推送到 Docker Hub
  3. Some more steps which would need Docker CLI需要 Docker CLI 的更多步骤

Example:例子:

pipeline {
  .
  .
  .
  stage("Docker Build") {
    steps {
      sh "docker build -t [user_name]/[image_name]:[tag] .
    }
  }
}

I'm quite new to this, and I have tried multiple ways to install docker and it was unsuccessful and it is a bad practice too.我对此很陌生,我尝试了多种安装 docker 的方法,但没有成功,这也是一种不好的做法。

We can run docker run -v /var/run/docker.sock:/var/run/docker.sock... but I can't use bind mounting while using docker build command.我们可以运行docker run -v /var/run/docker.sock:/var/run/docker.sock...但我不能在使用 docker 构建命令时使用绑定安装。

Can someone please suggest me a way where I can use docker commands inside Jenkins SSH Agents?有人可以建议我在 Jenkins SSH 代理中使用 docker 命令的方法吗?

Solution解决方案

  1. Install Docker CLI without the Daemon in Jenkins Docker Slave.在 Jenkins Docker 从站中安装没有守护程序的 Docker CLI。 I have used this Docker Agent and installed Docker CLI inside it using this method我已使用此Docker 代理并使用此方法在其中安装了 Docker CLI
  2. Then as a docker daemon I've used my remote docker host.然后作为 docker 守护程序,我使用了我的远程 docker 主机。 (Also, you can configure the local docker host as remote using these steps ). (此外,您可以使用这些步骤将本地 docker 主机配置为远程主机)。 You can use docker remote host using --host flag.您可以使用 --host 标志使用 docker 远程主机。 Eg docker --host xxxx:2375 build -t johndoe:calculator.例如docker --host xxxx:2375 build -t johndoe:calculator.
  3. Syntax: docker --host [Docker_Host]:[Port] build -t [Image_Name]:[Image_Tag].语法: docker --host [Docker_Host]:[Port] build -t [Image_Name]:[Image_Tag].

My New Jenkinsfile is as follows:我的新 Jenkinsfile 如下:

pipeline {
  agent any

  stages {
    stage("Install") {
      steps {
        sh "npm install"
      }
    }
    stage("Build") {
      steps {
        sh "npm run build"
      }
    }
    stage("Format") {
        steps {
            sh "npm run format"
        }
    }
    stage("Lint") {
      steps {
        sh "npm run lint"
      }
    }
    stage("Test") {
      steps {
        sh "npm run test"
      }
    }
    stage("Code Coverage") {
      steps {
        sh "npm run test:cov"
        publishHTML(target: [
            reportDir: "./coverage/lcov-report",
            reportFiles: "index.html",
            reportName: "Jest Coverage Report"
        ])
      }
    }
    stage("End-To-End Testing") {
      steps {
        sh "npm run test:e2e"
      }
    }
    stage("Docker Build") {
      steps {
        withCredentials([string(credentialsId: 'Docker_Host', variable: 'DOCKER_HOST')]) {
          sh 'docker --host $DOCKER_HOST build -t xxx/xxx .'
        }
      }
    }
  }
}

Note: I have stored Docker host URL on Jenkins as a credential and used it using withCredentials function. Note: I have stored Docker host URL on Jenkins as a credential and used it using withCredentials function.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM