问题
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