How to spawn remote docker container from a scripted jenkinsfile only by name of image
Spawning container an remote docker host works when we are using agent labels/templates and a jenkins freestyle project as you have shown in many tutorials. But when we are going to a multibranch pipeline project, there seems no way to execute any command in the newly spawned (independed) remote container.
Does anyone has any experience using multibranch pipeline jobs with scripted jenkinsfile? We don't want to use these jenkins agent labels, because we have great number of different images for our build jobs. It would be really tedious to configure 'every' day new docker templates using jenkins gui. We like our developer-teams to edit the image name in the jenkinsfile and commit it to the project repository.
When trying to use a scripted jenkinsfile like these little PoC it always happens, that a new container is spawned, but the three 'sh' commands are executed within the jenkins-master container instead of the newly spawned container.
node {
docker.withServer('tcp://192.168.122.1:4243') {
docker.image('bash:latest').withRun() {
sh 'hostname -I'
sh 'sleep 60s'
sh 'hostname -I'
}
}
}
Any help apreciated.
-
I found an answer why the sh commands are not executed in the remote container here https://go.cloudbees.com/docs/plugins/docker-workflow/
"Unlike
inside
, shell steps inside the block are not run inside the container, but they could connect to it using a local TCP port for example.!It's the default behavior of image.withRun().
0 -
But still there is the question how to implement this scenario?
We want to build jenkins jobs of type multibranch pipeline in containers and make use of jenkinsfile (preferrably of declarative jenkinsfile). These containers should be hosted independently (no container in container) of the jenkins-master on a remote docker host. The name of the image to spawn should be defined in the jenkinsfile in order to store it in the repository. We don't want to use these tedious image label feature of jenkins, because we don't want to update the labels in the jenkins gui for each new image we prepare.
0 -
As you figured out in order for `sh` commands to run inside the container you need to use `inside`, not `withRun`. However, the `inside` and `build` commands do not work properly with remote Docker, see https://jenkins.io/doc/book/pipeline/docker/#using-a-remote-docker-server
It should probably be possible to let Docker do the work by writing the `Dockerfile` to do the build and then using `docker build`.
0 -
If you only want to ref the image, you will need to configure the other details. We haven't used this (because we actually like to specify the details) but this is what you would need to pre-configure the other items so that you can ref the image directly:
https://go.cloudbees.com/docs/plugins/docker-workflow/
"this plugin provides a way to run build steps inside an arbitrary image. In the simplest case, just select an
image
and run your whole buildinside
it:"docker.image('maven:3.3.3-jdk-8').inside { git '…your-sources…' sh 'mvn -B clean install' }
0
Please sign in to leave a comment.
Comments
4 comments