18

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 4

22

You can use the second argument of withRun

.withRun('-p 3000:3000', '--arg1 somearg --arg2 anotherarg')
Sign up to request clarification or add additional context in comments.

4 Comments

Perfect!!! I looked around for a long time trying to find a proper reference for this command and others. Do you know where I'd find that? How did you know about the second argument for withRun()?
Maybe they should put the documentation in a more straight forward location.. Just sayin
There's like no documentation on this. Anyone find any?
5

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

1 Comment

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

1 Comment

Now how to do this using the declarative syntax?
1

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.