首页 > 解决方案 > Jenkins Docker Declarative .withRun

问题描述

I am using the declarative format for pipeline files and running inside of a docker container that is defined using a Dockerfile in my project's root directory.

My Jenkinsfile looks like this:

pipeline {
  agent {
    dockerfile {
      additionalBuildArgs '--network host'
    }
  }
  stages {
    stage('Test') {
      steps {
        sh 'pytest --version'
      }
    }
}

I would like to pass additional arguments to the docker run command similar to this question ... How to pass docker container arguments when running the image in a Jenkinsfile

Is it possible to do that in the declarative pipeline format, or should I switch?

Edit:

This is essentially the equivalent of what I am trying to do in non-declarative:

node {
  def pytestImage = docker.build('pytest-image:latest', '--network host .')

  pytestImage.inside('--network=host') {
    sh 'pytest --version'
    // other commands ...
  }
}

标签: dockerjenkinsjenkins-pipelinejenkins-declarative-pipeline

解决方案


您可以将args选项添加到您的dockerfile. 它将参数直接传递给docker run调用:

pipeline {
  agent {
    dockerfile {
      additionalBuildArgs '--network host'
      args '--network=host'
    }
  }

  stages {
    stage('Test') {
      steps {
        sh 'pytest --version'
      }
    }
}

更多信息在这里


推荐阅读