title: What is the proper way to stop and remove a docker container in Jenkins when using Docker Pipeline Plugin?
body: <p>I have a Jenkins pipeline that builds and runs a Docker machine, not as an agent, but using a scripting block along with the Docker Pipeline Plugin methods <code>docker.build()</code> and <code>Image.run()</code>. This works fine but if the build fails, the docker container is left running! I currently have <code>Container.stop()</code> in a <code>post{ always{} }</code> block but it doesn't seem to work. I don't want ssh into my Jenkins server to delete the container after every build and I can't just leave it because it has a specific and necessary name. <strong>How do I stop and rm the container regardless of failure of the build?</strong></p>
  
  <p>My pipeline:</p>
  
  <pre><code>pipeline {
      agent none
      stages {
          stage('Checkout') {
              agent any
              steps {
                  checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '&lt;some credentials&gt;', url: '&lt;a git repo&gt;']]])
              }
          }
          stage('Spin Up Receiver') {
              agent any
              steps {
                  script { 
                      def receiver = docker.build("receiver",  "--rm centos7_receiver")
                      def receiver_container = receiver.run("-d -v ${PWD}/realtime_files/station_name/201707/f/20170710_191:/DSK1/SSN/LOG0_f/17001 --network='rsync_test' --name='test_receiver'")
                  }
              }
          }
          stage('Run Tests') {
              agent { dockerfile { args '-v /etc/passwd:/etc/passwd --network="rsync_test"' } }
              steps {
                  sh "python ./rsyncUnitTests.py"
              }
          }
      }
      post {
          always {
              script { 
                  receiver_container.stop()
              }
          }
          failure {
              sendEmail('foo@bar.com')
          }
          changed {
              sendEmail('foo@bar.com')
          }
      }
  }
  </code></pre>