Jenkins管道应该在部署后删除远程守护进程上的容器,我想让它继续运行

时间:2018-02-13 14:42:34

标签: docker jenkins jenkins-pipeline daemon

我正在尝试使用jenkins管道构建部署代码,并使用远程docker守护程序进行部署。 一切正常,但jenkins管道正在停止并在管道脚本结束后删除所有容器。在该容器停止并移除后,服务器将持续10秒钟。

   stage {
        steps {
                 script {
                           docker.withServer('tcp://10.10.10.10:2375') {
                           docker.withRegistry('https://registry.my.com/','jenkins-registry') {
                           docker.image('registry.my.com/image-my/my:latest').withRun(' -p 9090:80 -i -t --name harpal ') {
                                                         sh 'docker ps -a'
                                    }
                            }
                       }
               }
        }

输出

[Flights-Docker-POC] Running shell script
+ docker ps -a
CONTAINER ID        IMAGE                                     COMMAND                  CREATED             STATUS                  PORTS                  NAMES
6a4c5094a8d2        registry.my.com/image-my/my:latest   "/usr/bin/supervisord"   6 hours ago         Up Less than a second   0.0.0.0:9090->80/tcp   harpal
[Pipeline] sh
[Flights-Docker-POC] Running shell script
+ docker stop 6a4c5094a8d22179b364ee2d3b97e998a2c13e8b136c55816c0d8f838c17248b
6a4c5094a8d22179b364ee2d3b97e998a2c13e8b136c55816c0d8f838c17248b
+ docker rm -f 6a4c5094a8d22179b364ee2d3b97e998a2c13e8b136c55816c0d8f838c17248b
6a4c5094a8d22179b364ee2d3b97e998a2c13e8b136c55816c0d8f838c17248b

2 个答案:

答案 0 :(得分:1)

got the answer for it.it wasn't issue related to entry point in my image
was suppose to use image.run() method instead of withRun(), withRun() method internally calls run() method and stops container in finally block of its implementation. 

 public <V> V withRun(String args = '', Closure<V> body) {
            docker.node {
                Container c = run(args)
                try {
                    body.call(c)
                } finally {
                    c.stop()
                }
            }
        }
btw thank you guys for help.

script was supposed to be like.

       stage {
            steps {
                     script {
                               docker.withServer('tcp://10.10.10.10:2375') {
                               docker.withRegistry('https://registry.my.com/','jenkins-registry') {
                               docker.image('registry.my.com/image-my/my:latest').run(' -p 9090:80 -i -t --name harpal ') 

    }
                           }
                   }
            }

答案 1 :(得分:0)

我不相信有一种方法可以使用Docker插件Groovy类来保持它的存活,它打算在运行后删除容器。

如果您只是尝试从Jenkins启动Docker容器,只需使用shell命令执行

sh 'docker run -p 9090:80 -i -t --name harpal registry.my.com/image-my/my:latest '

如果您试图让容器保持活动状态以进行调试并环顾四周,我通常会添加

sh 'sleep 30m'

然后转到Docker机器,用

查看容器
docker exec -it <ContainerID> bash