Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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'
    }
}
like image 259
Sean Lynch Avatar asked Sep 13 '17 20:09

Sean Lynch


People also ask

Can you pass arguments to Dockerfile?

Here is the Dockerfile using both ARG and ENV together to take the PORT as the build argument. Here is the app running on the port 3090. In this way, we can pass as many arguments as possible to pass as environment variables while building the image.

Can we use Docker container as a node in Jenkinsfile?

Many organizations use Docker to unify their build and test environments across machines, and to provide an efficient mechanism for deploying applications. Starting with Pipeline versions 2.5 and higher, Pipeline has built-in support for interacting with Docker from within a Jenkinsfile .

How will you run a container along with an image within the container?

To run an image inside of a container, we use the docker run command. The docker run command requires one parameter and that is the image name. Let's start our image and make sure it is running correctly. Execute the following command in your terminal.


2 Answers

You can use the second argument of withRun

.withRun('-p 3000:3000', '--arg1 somearg --arg2 anotherarg')
like image 118
Tarun Lalwani Avatar answered Sep 23 '22 13:09

Tarun Lalwani


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

like image 42
Sheshtawy Avatar answered Sep 22 '22 13:09

Sheshtawy