4

I have a Jenkinsfile or a Jenkins pipeline which creates a new image and starts a container out of that image. It works well for the first time. But on subsequent runs, I want the previous container to be stopped and removed. My Jenkinsfile is as follows:

node {
   def commit_id
   stage('Preparation') {
     checkout scm
     sh "git rev-parse --short HEAD > .git/commit-id"                        
     commit_id = readFile('.git/commit-id').trim()
   }
   stage('docker build/push') {
     docker.withRegistry('https://index.docker.io/v1/', 'dockerhub') {
       def app = docker.build("my-docker-id/my-api:${commit_id}", '.').push()
     }
   }
   stage('docker stop container') {
       def apiContainer = docker.container('api-server')
       apiContainer.stop()
   }
   stage('docker run container') {
       def apiContainer = docker.image("my-docker-id/my-api:${commit_id}").run("--name api-server --link mysql_server:mysql --publish 3100:3100")
   }
}

The stage 'docker stop container' is failing. That is because I don't know the right API to get the container and stop it. Thanks.

vijayst
  • 20,359
  • 18
  • 69
  • 113

2 Answers2

12

As in this Jenkinsfile, you can use sh commands instead.

That way, you can use lines like:

sh 'docker ps -f name=zookeeper -q | xargs --no-run-if-empty docker container stop'
sh 'docker container ls -a -fname=zookeeper -q | xargs -r docker container rm'

That would ensure a container x (here named zookeper), if it was running, is first stopped and removed.

Michael A. points out in the comments this is not a proper solution, and assume docker being installed on the slave.
He refers to jenkinsci/plugins/docker/workflow/Docker.groovy, but a container method for the Docker class is not implemented yet.


Update August 2018:

Pieter Vogelaar points out in the comments to "Jenkinsfile Docker pipeline multi stage" he wrote about:

By using a global pipelineContext object, it's possible to use the returned container object in a further stage.

It is:

pipelineContext global variable which is of type LinkedHashMap.
The Jenkinsfile programming language is Groovy. In the Groovy this comes close to the equivalent of the JavaScript object. This variable makes it possible to share data or objects between stages.

So this is a Declarative Pipeline, starting with:

// Initialize a LinkedHashMap / object to share between stages
def pipelineContext = [:]
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks, I used something simpler. `sh 'docker container rm -f api-server'` – vijayst Dec 16 '17 at 07:57
  • This is very bad solution. Because you execute it via external call instead of proper API call. You also will need Jenkins slave with docker tool installed and connected to remote Docker - so you connect it twice. – Michael A. Jun 16 '18 at 12:01
  • @MichaelA. OK, but what would be the proper API call in this case? – VonC Jun 16 '18 at 12:05
  • No proper call. So no good solution. I guess we came up with that we have to contribute to docker-workflow-plugin ourselves. I am investigating now into it... https://github.com/jenkinsci/docker-workflow-plugin/blob/master/src/main/resources/org/jenkinsci/plugins/docker/workflow/Docker.groovy – Michael A. Jun 16 '18 at 12:49
  • 1
    @MichaelA. OK. I have included your comment in the answer for more visibility. – VonC Jun 16 '18 at 16:26
  • There is a solution with the current available code! See http://pietervogelaar.nl/jenkinsfile-docker-pipeline-multi-stage . By using a global pipelineContext object, it's possible to use the returned container object in a further stage. – Pieter Vogelaar Jul 31 '18 at 12:16
  • @PieterVogelaar Great! I have included your comment in the answer for more visibility. – VonC Jul 31 '18 at 12:26
0

By using a global pipelineContext object, it's possible to use the returned container object in a further stage. Or for example in the post build step that must always be executed, also for a failed build. This way Docker containers are always stopped and removed at the very end of a build. I described a working solution at http://pietervogelaar.nl/jenkinsfile-docker-pipeline-multi-stage.

Pieter Vogelaar
  • 365
  • 2
  • 4
  • 16