2

I have a pipeline starting with:

pipeline {
    agent {
        docker {
            image 'ubuntu'
        }
    }

At the end of the build, success or failure, the pipeline ends with:

$ docker stop --time=1 922fc93c1ff65758270cd2ac9dbdc30549ea8acb000ce5982dfa1fc4a9ed79a0
$ docker rm -f 922fc93c1ff65758270cd2ac9dbdc30549ea8acb000ce5982dfa1fc4a9ed79a0

Is there a way to disable the rm -f step.

This is for debugging purpose. At the moment, I put a sleep somewhere which is very inconvenient.

David Bensoussan
  • 2,887
  • 2
  • 38
  • 55

3 Answers3

3

Thanks to @davidxxx to have put me on track of a solution.

The answer he suggested doesn't require to share the docker socket. As I wanted a debugging solution, what i came up with is fine, as it is not going into production. What he offers is more verbose but is safer. So it really boils down to what you need.

My solution: In the pipeline, share the socket with the newly created container

pipeline {
    agent {
        docker {
            image 'ubuntu'
            args '-v /var/run/docker.sock:/var/run/docker.sock'
        }
    }

Then, in the job, create a new image out of the one you are running:

docker commit $(basename $(cat /proc/1/cpuset)) foo 

$(basename $(cat /proc/1/cpuset)) gets the hash of the current container as found here

foo being the name of the new image

David Bensoussan
  • 2,887
  • 2
  • 38
  • 55
2

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.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • Thanks for all the details about the plugin. That's what I thought. I think I found a workaround though: Share the docker socket with the `args` tag and commit the current container. I can get the hash with `$(basename $(cat /proc/1/cpuset))`. I'll test and update the post – David Bensoussan Feb 26 '20 at 19:32
  • You are welcome. Committing the container looks fine (I refer that at the end of my answer). About the docker socket , if you deploy Jenkins as a container, you can directly mount it as a volume when you run that ( `-v /var/run/docker.sock:/var/run/docker.sock`). If you found a good solution, don't hesitate to add your own answer, that is better than editing the question itself :) – davidxxx Feb 26 '20 at 19:53
-3
  1. Delete all Docker Containers
    docker rm container $(docker container ls -aq) 
      note:--(if you want add super user)
    
  2. Delete all Docker Images
    docker rmi  $(docker images -aq) to delete all docker images
      note:--(if you want add super user)
    
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61