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'
}
}
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.
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 .
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.
You can use the second argument of withRun
.withRun('-p 3000:3000', '--arg1 somearg --arg2 anotherarg')
Use .withRun('-p 3000:3000', '--arg1 arg1 --arg2 arg2')
. The documentation for this is in the docker-workflow-plugin here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With