2017-10-15 167 views
2

我使用码头集装箱的Jenkins。我想在Jenkins管道中构建docker镜像,但docker不存在于此容器中(其中Jenkins)。在Jenkins(码头集装箱)管道中建造码头图像

詹金斯集装箱通过码头工人撰写,YML文件部署:

version: "3.3" 
services: 
    jenkins: 
    image: jenkins:alpine 
    ports: 
     - 8085:8080 
    volumes: 
     - ./FOR_JENKINS:/var/jenkins_home 

我们可以做詹金斯管道建设码头工人的形象? 我们可以使用docker部署一些docker容器并使用一次构建docker镜像吗?或者是其他东西?你如何对待他们?

编辑:

感谢@VonC,我查了你的信息,但是...... “权限被拒绝”

码头工人撰写文件:

version: "3.3" 
services: 
    jenkins: 
    image: jenkins:alpine 
    ports: 
     - 8085:8080 
    volumes: 
     - ./FOR_JENKINS:/var/jenkins_home 
#  - /var/run/docker.sock:/var/run/docker.sock:rw 
     - /var/run:/var/run:rw 

Jenkinsfile:

pipeline { 
    agent any 

    stages { 
     stage('Build') { 
      steps { 
       echo "Compiling..." 
       sh "${tool name: 'sbt', type: 'org.jvnet.hudson.plugins.SbtPluginBuilder$SbtInstallation'}/bin/sbt compile" 
      } 
     } 
     /*stage('Unit Test') { 
      steps { 
       echo "Testing..." 
       sh "${tool name: 'sbt', type: 'org.jvnet.hudson.plugins.SbtPluginBuilder$SbtInstallation'}/bin/sbt coverage 'test-only * -- -F 4'" 
       sh "${tool name: 'sbt', type: 'org.jvnet.hudson.plugins.SbtPluginBuilder$SbtInstallation'}/bin/sbt coverageReport" 
       sh "${tool name: 'sbt', type: 'org.jvnet.hudson.plugins.SbtPluginBuilder$SbtInstallation'}/bin/sbt scalastyle || true" 
      } 
     }*/ 
     stage('DockerPublish') { 
      steps { 
       echo "Docker Stage ..." 
       // Generate Jenkinsfile and prepare the artifact files. 
       sh "${tool name: 'sbt', type: 'org.jvnet.hudson.plugins.SbtPluginBuilder$SbtInstallation'}/bin/sbt docker:stage" 

       echo "Docker Build-2 ..." 
       // Run the Docker tool to build the image 
       script { 
        docker.withTool('docker') { 

         echo "D1- ..." 
         //withDockerServer([credentialsId: "AWS-Jenkins-Build-Slave", uri: "tcp://192.168.0.29:2376"]) { 
          echo "D2- ..." 
          sh "printenv" 
          echo "D3- ..." 
          //sh "docker images" 
          echo "D4- ..." 
          docker.build('my-app:latest', 'target/docker/stage').inside("--volume=/var/run/docker.sock:/var/run/docker.sock") 
          echo "D5- ..." 
          //base.push("tmp-fromjenkins") 
         //} 

        } 
       } 
      } 
     } 
    } 
} 

结果:

[job1] Running shell script 

+ docker build -t my-app:latest target/docker/stage 

Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post http://%2Fvar%2Frun%2Fdocker.sock/v1.29/build?buildargs=%7B%7D&cachefrom=%5B%5D&cgroupparent=&cpuperiod=0&cpuquota=0&cpusetcpus=&cpusetmems=&cpushares=0&dockerfile=Dockerfile&labels=%7B%7D&memory=0&memswap=0&networkmode=default&rm=1&shmsize=0&t=my-app%3Alatest&target=&ulimits=null: dial unix /var/run/docker.sock: connect: permission denied 

script returned exit code 1 

编辑: 最后问题拒绝 “权限” 固定:

>>sudo chmod 0777 /var/run/docker.sock 

曾为国家:

呼叫在主机:

>>sudo chmod 0777 /var/run/docker.sock 

泊坞窗撰写文件:

version: "3.3" 
services: 
    jenkins: 
    image: jenkins:alpine 
    ports: 
     - 8085:8080 
    volumes: 
     - ./FOR_JENKINS:/var/jenkins_home 
#  - /var/run/docker.sock:/var/run/docker.sock:rw 
     - /var/run:/var/run:rw 

Jenkinsfile:

pipeline { 
    agent any 

    stages { 
     stage('Build') { 
      steps { 
       echo "Compiling..." 
       sh "${tool name: 'sbt', type: 'org.jvnet.hudson.plugins.SbtPluginBuilder$SbtInstallation'}/bin/sbt compile" 
      } 
     } 
     /*stage('Unit Test') { 
      steps { 
       echo "Testing..." 
       sh "${tool name: 'sbt', type: 'org.jvnet.hudson.plugins.SbtPluginBuilder$SbtInstallation'}/bin/sbt coverage 'test-only * -- -F 4'" 
       sh "${tool name: 'sbt', type: 'org.jvnet.hudson.plugins.SbtPluginBuilder$SbtInstallation'}/bin/sbt coverageReport" 
       sh "${tool name: 'sbt', type: 'org.jvnet.hudson.plugins.SbtPluginBuilder$SbtInstallation'}/bin/sbt scalastyle || true" 
      } 
     }*/ 
     stage('DockerPublish') { 
      steps { 
       echo "Docker Stage ..." 
       // Generate Jenkinsfile and prepare the artifact files. 
       sh "${tool name: 'sbt', type: 'org.jvnet.hudson.plugins.SbtPluginBuilder$SbtInstallation'}/bin/sbt docker:stage" 

       echo "Docker Build-2 ..." 
       // Run the Docker tool to build the image 
       script { 
        docker.withTool('docker') { 

         echo "D1- ..." 
         //withDockerServer([credentialsId: "AWS-Jenkins-Build-Slave", uri: "tcp://192.168.0.29:2376"]) { 
          echo "D2- ..." 
          sh "printenv" 
          echo "D3- ..." 
          //sh "docker images" 
          echo "D4- ..." 
          docker.build('my-app:latest', 'target/docker/stage') 
          echo "D5- ..." 
          //base.push("tmp-fromjenkins") 
         //} 

        } 
       } 
      } 
     } 
    } 
} 

我的决心:

我加入Jenkinsfile一些步骤,并得到:

pipeline { 
    agent any 
    //def app 

    stages { 
     stage('Build') { 
      steps { 
       echo "Compiling..." 
       sh "${tool name: 'sbt', type: 'org.jvnet.hudson.plugins.SbtPluginBuilder$SbtInstallation'}/bin/sbt compile" 
      } 
     } 

     stage('DockerPublish') { 
      steps { 
       echo "Docker Stage ..." 
       // Generate Jenkinsfile and prepare the artifact files. 
       sh "${tool name: 'sbt', type: 'org.jvnet.hudson.plugins.SbtPluginBuilder$SbtInstallation'}/bin/sbt docker:stage" 

       echo "Docker Build ..." 
       // Run the Docker tool to build the image 
       script { 
        docker.withTool('docker') { 

          echo "Environment:" 
          sh "printenv" 
          app = docker.build('ivanbuh/myservice:latest', 'target/docker/stage') 
          echo "Push to Docker repository ..." 
         docker.withRegistry('https://registry.hub.docker.com', 'docker-hub-credentials') { 
           app.push("${env.BUILD_NUMBER}") 
           app.push("latest") 
          } 

          echo "Complated ..." 
        } 
       } 
      } 
     } 
//https://boxboat.com/2017/05/30/jenkins-blue-ocean-pipeline/ 
//https://gist.github.com/bvis/68f3ab6946134f7379c80f1a9132057a 
     stage ('Deploy') { 
      steps { 
       sh "docker stack deploy myservice --compose-file docker-compose.yml" 
      } 
     } 
    } 
} 
+0

您是否在特权模式下运行docker? (https://github.com/rancher/jenkins-master#running-docker-server-within-docker) – VonC

+0

在Docker Compose文件中不支持属性“特权”。 –

+0

好的,那么我想现在你的chown是一个很好的解决方法。我在答案中引用了它。 – VonC

回答

2

你可以看一下 “Docker in Docker in Jenkins pipeline”。它包括步骤:

在Jenkinsfile中,我需要将我的构建容器连接到外部Docker实例。这是通过安装多克尔插座本身完成:

docker.build('my-build-image').inside("--volume=/var/run/docker.sock:/var/run/docker.sock") { 
    // The build here 
} 

你可以看到一个类似的方法在“Building containers with Docker in Docker and Jenkins”。

为了使主机系统的Docker可用,我需要使API可用于Jenkins docker容器。您可以通过映射父系统上可用的docker套接字来完成此操作。
我创建了一个小码头工人,撰写文件,在那里我我的两个卷和泊坞窗插座如下图:

jenkins: 
    container_name: jenkins 
    image: myjenkins:latest 
    ports: 
    - "8080:8080" 
    volumes: 
    - /Users/devuser/dev/docker/volumes/jenkins:/var/jenkins_home 
    - /var/run:/var/run:rw 

请注意与rw特权的特殊映射“/var/run”,这是需要确保Jenkins容器可以访问主机系统docker.sock

而且,与我mentioned before一样,您可能需要在特权模式下运行docker。
或者,如OP所报道的:

sudo chmod 0777 /var/run/docker.sock 
+0

我在第一篇文章写的下一个方法 –

+0

@IvanBukharin太棒了!不要忘记阅读https://stackoverflow.com/help/someone-answers – VonC