如何使用SSH直接连接到远程Docker容器 [英] How to connect directly to a remote docker container with ssh

查看:822
本文介绍了如何使用SSH直接连接到远程Docker容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想直接使用ssh连接到远程运行的Docker容器.通常我可以

I want to connect to a remote running Docker container directly with ssh. Normally I can

$ ssh -i privateKey user@host
$ docker ps #which will list all running containers
$ docker exec -it ***** bash deploy.sh # ***** is container id and this line run a deployment script 

但是我需要从Jenkins管道中运行此脚本,而我只有一次机会.经过多次尝试,我想出了这个

But I need to run this script from a Jenkins pipeline where I have only one chance. After many trying, I come up with this

$ ssh -tt -i ~/privateKey user@host docker exec -it $(docker ps | grep  unique_text | cut -c1-10) /bin/bash deploy.sh

因为它返回而没有帮助我的困境

Which have not help my plight because it returns

"docker exec" requires at least 2 arguments.

这实际上意味着命令在这里被截断了$(docker ps | grep ...

Which actually mean the command is truncated here $(docker ps | grep ...

我的解决方案

sh 'ssh -tt -i $FILE -o StrictHostKeyChecking=no $USER@$HOST /bin/bash -c \'"docker exec -it $(docker ps | grep unique_text | cut -c1-10) bash start.sh"\''

推荐答案

此问题的最佳解决方案是在Jenkins中创建一个节点

The best solution to this problem is to create a node in Jenkins

步骤1-转到管理Jenkins"部分,然后向下滚动到管理节点"部分.

Step 1 − Go to the Manage Jenkins section and scroll down to the section of Manage Nodes.

步骤2-单击新节点

Step 2 − Click on New Node

第3步-为节点命名,选择Dumb slave选项,然后单击Ok.

Step 3 − Give a name for the node, choose the Dumb slave option and click on Ok.

步骤4-输入节点从属计算机的详细信息.在下面的示例中,我们将从机视为Windows机器,因此选择了让Jenkins作为Windows服务控制该Windows从机"选项作为启动方法.我们还需要添加从属节点的必要详细信息,例如节点名称和节点计算机的登录凭据.单击保存按钮.输入名称为"New_Slave"的标签可用于配置作业以使用此从属计算机.

Step 4 − Enter the details of the node slave machine. In the below example, we are considering the slave machine to be a windows machine, hence the option of "Let Jenkins control this Windows slave as a Windows service" was chosen as the launch method. We also need to add the necessary details of the slave node such as the node name and the login credentials for the node machine. Click the Save button. The Labels for which the name is entered as "New_Slave" is what can be used to configure jobs to use this slave machine.

完成上述步骤后,新节点计算机最初将处于脱机状态,但是如果正确输入了先前屏幕中的所有设置,则它将变为联机状态.如果需要,可以随时使节点从属计算机脱机.

Once the above steps are completed, the new node machine will initially be in an offline state, but will come online if all the settings in the previous screen were entered correctly. One can at any time make the node slave machine as offline if required.

在我的詹金斯管道中

node("build_slave"){
    sh 'docker exec -it $(docker ps | grep unique_text | cut -c1-10) bash deploy.sh'
}

这篇关于如何使用SSH直接连接到远程Docker容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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