开发者社区> 问答> 正文

Jenkins Kubernetes插件:如何从Dockerfile构建图像并在图像中运行步骤

我正在使用Jenkins kubernetes-plugin。是否可以从Dockerfile构建docker镜像,然后在创建的图像中运行步骤?该插件需要在pod模板中指定一个图像,所以我的第一次尝试是使用docker-in-docker,但步骤docker.image('jenkins/jnlp-slave').inside() {..}失败:

pipeline {
agent {

kubernetes {
  //cloud 'kubernetes'
  label 'mypod'
  yaml """

apiVersion: v1
kind: Pod
spec:
containers:

  • name: docker
    image: docker:1.11
    command: ['cat']
    tty: true
    volumeMounts:

    • name: dockersock
      mountPath: /var/run/docker.sock

    volumes:

  • name: dockersock
    hostPath:
    path: /var/run/docker.sock
    """
    }
    }

stages {

stage('Build Docker image') {
  steps {
    git 'https://github.com/jenkinsci/docker-jnlp-slave.git'
    container('docker') {
      sh "docker build -t jenkins/jnlp-slave ."
      docker.image('jenkins/jnlp-slave').inside() {
        sh "whoami"
      }
    }
  }
}

}
}
失败:

WorkflowScript: 31: Expected a symbol @ line 31, column 11.

         docker.image('jenkins/jnlp-slave').inside() {

展开
收起
k8s小能手 2019-02-22 16:05:51 3208 0
1 条回答
写回答
取消 提交回答
  • 整合最优质的专家资源和技术资料,问答解疑

    pipeline {
    agent {

    kubernetes {
      //cloud 'kubernetes'
      label 'mypod'
      yaml """

    apiVersion: v1
    kind: Pod
    spec:
    containers:

    • name: docker
      image: docker:1.11
      command: ['cat']
      tty: true
      volumeMounts:

      • name: dockersock
        mountPath: /var/run/docker.sock

      volumes:

    • name: dockersock
      hostPath:
      path: /var/run/docker.sock
      """
      }
      }

    stages {

    stage('Build Docker image') {
      steps {
        git 'https://github.com/jenkinsci/docker-jnlp-slave.git'
        container('docker') {
          script {
            def image = docker.build('jenkins/jnlp-slave')
            image.inside() {
              sh "whoami"
            }
          }
        }
      }
    }

    }
    }

    2019-07-17 23:29:12
    赞同 1 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
基于ACK One 实现简单的跨云协同 让业务管理更高效 立即下载
通过 ACK 智能化运维体系获得集群自动化诊断和自愈能力 立即下载
Alibaba Cloud CodePipeline 基于Jenkins的CI / CD探索之路 立即下载