First of all, I'm not sure if you want to keep the container in the block because the container will be stopped as soon as its body exits if you use withRun
.
So you can run the COMMAND that specified after the IMAGE can be run within either Image.run([arg, command])
or withRun
according to Jenkins pipeline-syntax page.
$ docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Image.withRun[(args[, command])] {…}
Like run but stops the container as soon as its body exits, so you do not need a try-finally block.
Image.run([args, command])
Uses docker run to run the image, and returns a Container which you could stop later. Additional args may be added, such as '-p 8080:8080 --memory-swap=-1'. Optional command is equivalent to Docker command specified after the image.
If you see the Image.run()
by visiting Jenkins pipeline-syntax, it's described that you can run the image with Docker COMMAND.
For instance here's an example as below:
stages {
// docker run -p 3306:3306 -e MYSQL_ROOT_PASSWORD=password -d mysql:latest --default-authentication-plugin=mysql_native_password
stage("Run MySQL") {
steps {
script {
docker.image('mysql:latest').withRun('-p 3306:3306 -e "MYSQL_ROOT_PASSWORD=root"', '--default-authentication-plugin=mysql_native_password') {c ->
// Run command
}
}
}
}
Edit: I tried running the MySQL container using withRun()
with two parameters, and succeed to run the command. Try the code snippet above, and see the pipeline as shown in the attached file.