使用 Docker Pipeline Plugin 时,在 Jenkins 中停止和删除 docker 容器的正确方法是什么? [英] What is the proper way to stop and remove a docker container in Jenkins when using Docker Pipeline Plugin?

查看:19
本文介绍了使用 Docker Pipeline Plugin 时,在 Jenkins 中停止和删除 docker 容器的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个构建和运行 Docker 机器的 Jenkins 管道,不是作为代理,而是使用脚本块以及 Docker 管道插件方法 docker.build()Image.run().这工作正常,但如果构建失败,docker 容器将继续运行!我目前在 post{ always{} } 块中有 Container.stop() 但它似乎不起作用.我不希望 ssh 进入我的 Jenkins 服务器以在每次构建后删除容器,我不能只留下它,因为它有一个特定且必要的名称.无论构建失败,我如何停止和 rm 容器?

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 docker.build() and Image.run(). This works fine but if the build fails, the docker container is left running! I currently have Container.stop() in a post{ always{} } 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. How do I stop and rm the container regardless of failure of the build?

我的管道:

pipeline {
    agent none
    stages {
        stage('Checkout') {
            agent any
            steps {
                checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '<some credentials>', url: '<a git repo>']]])
            }
        }
        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')
        }
    }
}

推荐答案

这是一个可行的解决方案.您只需为主管道外部的容器定义一个变量.然后您可以在管道中的任何位置使用它来启动或停止容器.特别是,您可以删除 post{ always{ } } 中的容器.

Here is a working solution. You simply have to define a variable for the container outside the main pipeline. Then you can use it anywhere in the pipeline to start or stop the container. In particular, you can remove the container in post{ always{ } }.

def receiver_container
pipeline {
    agent any
    stages {
        stage('Checkout') {
            agent any
            steps {
                checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '<some credentials>', url: '<a git repo>']]])
            }
        }
        stage('Spin Up Receiver') {
            agent any
            steps {
                script { 
                    def receiver = docker.build("receiver",  "--rm receiver_docker")
                    receiver_container = receiver.run("-d -u 0:0 -v /var/lib/jenkins/workspace/RsyncRealtime/jenkins_rt:/DSK1/SSN/LOG5_F/17191 --network='rsync_test' --name='test_receiver'")
                }
            }
        }
        stage('Run Unit Tests') {
            agent { 
                dockerfile { 
                    args '-u 0:0 -v /etc/passwd:/etc/passwd --network="rsync_test"'
                } 
            }
            steps {
                sh "sshpass -p 'test' ssh anonymous@test_receiver ls -l /DSK1/SSN/LOG5_F/17191"
                sh "python ./rsyncUnitTests.py"
            }
        }
    }
    post {
        always {
            script { 
                receiver_container.stop()
            }
        }
        failure {
            sendEmail('foo@bar.com')
        }
        changed {
            sendEmail('foo@bar.com')
        }
    }
}

这篇关于使用 Docker Pipeline Plugin 时,在 Jenkins 中停止和删除 docker 容器的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆