如何使jenkins管道在远程服务器上运行docker命令

时间:2020-06-12 14:52:19

标签: docker jenkins ssh jenkins-pipeline

我有2个ec2实例 第一个:托管jenkins和docker,后者从jar文件构建映像,然后将其推送到docker hub repo

第二个:这将是我的应用程序服务器,其中将包含docker,它将从docker hub提取此映像并在docker引擎上运行容器

我已经完成了创建映像并将其推送到docker hub的第一部分

我的问题是如何执行从jenkins管道到我的应用程序服务器的docker命令,以从docker hub提取图像并运行

2 个答案:

答案 0 :(得分:0)

您可以使用Jenkins SSH插件(https://plugins.jenkins.io/ssh/)来添加一个构建步骤,以使用ssh在远程主机上执行shell脚本。 enter image description here

或运行 ssh user@host "docker pull <image> && docker run <image>"

答案 1 :(得分:0)

我找到了解决问题的方法

我在jenkins上使用了 SSH Steps插件插件

及其在管道中的编写如下:

    stage('run commands on remote server') {
        steps { 
            script {
                def remote = [:]
                remote.user = 'ec2-user'
                remote.host = 'public or private ip of the remote server'
                remote.name = 'ec2-user'
                remote.identityFile = 'path for your ec2 private key'
                remote.allowAnyHosts = 'true'
                sshCommand remote: remote, command: 'your command 1 here'
                sshCommand remote: remote, command: 'your command 2 here'
            }
        } 
    }