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

Viewed 21509

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'
    }
}
4 Answers

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

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'
        }
    }
}

I leave this post because it was asked in a comment above: The documentation for Docker Pipeline plugin can be accessed like this:

  • Go to any Pipeline Job, and on the bottom click on the link [Pipeline Syntax]
  • One the left menu choose Global variable Reference. The plugin has the docker variable (with supported methods)
Related