How to pass docker container arguments when running the image in a Jenkinsfile

依然范特西╮ 提交于 2020-01-14 07:41:08

问题


I have a Dockerfile that ends with

ENTRYPOINT ["node", "index.js"]
CMD ["--help"]

The index.js can take a couple different arguments and I also need to expose a port for the container so if I run it manually I do something like:

docker run -p 3000:3000 my_container:latest --arg1 somearg --arg2 anotherarg

How do I do this in a Jenkinsfile? My test will communicate with this container so it needs to be running before I run the test. I use withRun() get it running before the test runs but I don't see how to specify the --arg1 somearg --arg2 anotherarg

stage('TestMicroservice') {
    //
    // HOW DO I SPECIFY '--arg1 somearg --arg2 anotherarg'?
    //
    docker.image("my_container:latest").withRun('-p 3000:3000') {
        sh 'npm run test-microservice'
    }
}

回答1:


You can use the second argument of withRun

.withRun('-p 3000:3000', '--arg1 somearg --arg2 anotherarg')



回答2:


Use .withRun('-p 3000:3000', '--arg1 arg1 --arg2 arg2'). The documentation for this is in the docker-workflow-plugin here.




回答3:


Another way you pass container arguments is by using the inside method. Below is an example taken from https://jenkins.io/doc/book/pipeline/docker/#caching-data-for-containers (click on the Toggle Scripted Pipeline link to view it)

node {
    /* Requires the Docker Pipeline plugin to be installed */
    docker.image('maven:3-alpine').inside('-v $HOME/.m2:/root/.m2') {
        stage('Build') {
            sh 'mvn -B'
        }
    }
}


来源:https://stackoverflow.com/questions/46206215/how-to-pass-docker-container-arguments-when-running-the-image-in-a-jenkinsfile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!