You can achieve that but not with with the docker {}
syntax because that
Execute the Pipeline, or stage, with the given container which will be
dynamically provisioned
You have some other options such as label
and args
but no option will defeat the idea of the plugin : run the container, do your operations and stop and remove the container.
To override that behavior, you will have to perform yourself things done by the jenkins plugin :
- execute directly the docker client program in your pipeline (need to have the docker client on the Jenkins machine)
- write the commands to execute in the image
Something like :
stage('Docker execution') {
steps {
sh "CONTAINER_ID= $(docker run ubuntu:fooVersion)"
sh "docker exec -ti $CONTAINER_ID commandToExecuteOnContainer"
}
}
Note that it will create new images of ubuntu at each pipeline execution. So even for debugging purposes it may fast become a mess.
For debugging purposes, I think that invoking the sleep function of the Jenkins pipeline is both simpler and neater.
For example :
steps {
// ....
sleep(time:15,unit:"MINUTES")
}
If a specific time is not enough because you need to inspect the container state maybe after some hours or days, you could save its state by using the commit
subcommand of docker such as docker commit myContainerId
.
You could execute that command directly in the Jenkins pipeline (why not defining a debugging parameter as input of the pipeline) or execute it directly on a machine where the docker client accesses to the docker registry used by the Pipeline.