连接到服务器内的码头容器

发布于 2025-02-05 16:27:07 字数 1607 浏览 3 评论 0原文

我是服务器端操作的新手,我想编写一个API,该API在GPU服务器上启动培训模型,并允许将火车输出显示给客户端。 由于一个以上的人连接到服务器,因此我们在Docker容器中工作,因此我们执行的操作不会引起任何问题。 我想在未来的API中使用以下代码。代码只需使用SSH连接到主服务器。我试图将paramiko用于此目的。 然后,我想进入我的容器,激活该容器中的Anaconda环境,然后运行Python脚本以开始培训。

import paramiko


host = "xxx.xx.x.xxx"       # actual host IP
username = "support"
password = "password"

client = paramiko.client.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(host, username=username, password=password)
_stdin, _stdout, _stderr = client.exec_command("pwd")
_stdin.close()
print(_stdout.read().decode())              # It prints the directory in the main server --> /home/support 
print(_stderr.read().decode())
_stdin, _stdout, _stderr = client.exec_command("docker exec -it <container id> /bin/bash; pwd")
_stdin.close()
print(_stdout.read().decode())              # It still prints the directory in the main server --> /home/support. I guess it does not get into container.
print(_stderr.read().decode())              
_stdin, _stdout, _stderr = client.exec_command("docker exec -it <container id> /bin/bash; conda activate <my conda env>; python train.py")       # The real command series that I want to do.
_stdin.close()
print(_stdout.read().decode())
print(_stderr.read().decode())              # It prints the  
                                            # input device is not a TTY
                                            # bash: conda: command not found

client.close()

但这并不能像我想象的那样工作。我不知道如何进入我的容器。由于这种方法不起作用,因此我还试图将容器与SSH连接起来,但它也无法使用。

我的问题是如何执行所描述的操作。

I am very new to server side operations and I want to write an API that starts the training models on GPU server and allows the train outputs to be displayed to client.
Since more than one person is connected to the server, we work in docker containers so that the operations we do, do not cause any problems.
I want to use the code below in my future API. The code simply connect to main server with ssh. I tried to use Paramiko for that purpose.
Then I want to get into my container, activate my anaconda environment in that container and run a python script to start training.

import paramiko


host = "xxx.xx.x.xxx"       # actual host IP
username = "support"
password = "password"

client = paramiko.client.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(host, username=username, password=password)
_stdin, _stdout, _stderr = client.exec_command("pwd")
_stdin.close()
print(_stdout.read().decode())              # It prints the directory in the main server --> /home/support 
print(_stderr.read().decode())
_stdin, _stdout, _stderr = client.exec_command("docker exec -it <container id> /bin/bash; pwd")
_stdin.close()
print(_stdout.read().decode())              # It still prints the directory in the main server --> /home/support. I guess it does not get into container.
print(_stderr.read().decode())              
_stdin, _stdout, _stderr = client.exec_command("docker exec -it <container id> /bin/bash; conda activate <my conda env>; python train.py")       # The real command series that I want to do.
_stdin.close()
print(_stdout.read().decode())
print(_stderr.read().decode())              # It prints the  
                                            # input device is not a TTY
                                            # bash: conda: command not found

client.close()

But it does not work like how I imagine. I could not figure out how to get into my container. Since this approach does not work, I also tried to connect the container with ssh, but it did not work either.

My question is how can i perform such the operation I described.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅紫色的梦幻 2025-02-12 16:27:07

当您使用;作为命令之间的分离器时,它们成为主机上的单独命令。因此,在您的第一个命令中,pwd在主机上运行。

另外,由于您没有与TTY的容器进行交互,因此不应在此处使用-it选项。

尝试这两个命令

_stdin, _stdout, _stderr = client.exec_command("docker exec <container id> /bin/bash -c pwd")

_stdin, _stdout, _stderr = client.exec_command("docker exec <container id> /bin/bash -c 'conda activate <my conda env> && python train.py'")       # The real command series that I want to do.

然后我在命令中更改了&lt; image id&gt; &lt; code>&lt;图像。这可能是您的意思,所以我认为这不是您的问题的来源。

When you use ; as the separator between commands, they become separate commands on the host. So in your first command, the pwd is run on the host.

Also, the -it option shouldn't be used here, since you're not interacting with the container from your tty.

Try these two commands

_stdin, _stdout, _stderr = client.exec_command("docker exec <container id> /bin/bash -c pwd")

and

_stdin, _stdout, _stderr = client.exec_command("docker exec <container id> /bin/bash -c 'conda activate <my conda env> && python train.py'")       # The real command series that I want to do.

I've changed <image id> to <container id> in the commands since what you're interacting with is a container and not an image. It's probably what you meant, so I don't think that's where your issue comes from.

风吹雪碎 2025-02-12 16:27:07

我不太了解Paramiko。可能是切换到终端内部的终端并正确处理该终端的问题。

我将专注于直接使用SSH登录容器。当然,只有在您的网络上可以触及容器时,这才是。 (但是我认为是这样,因为您提到了多个在服务器上工作的人。

您必须确保Docker容器在您的网络上具有SSH端口(可能的安全风险!),并且您知道凭据(或更安全的安全性)甚至:切换到基于证书的登录)。

I do not know paramiko very well. Could be that it has trouble to switch to a terminal inside a terminal and handle that correctly.

I'd focus on log into the containers directly with ssh. That is of course only if the containers are reachable on your network. (But I assume so, since you mentioned multiple people working on the server.

You'd have to make sure, that the docker containers have ssh port exposed to your network (possible security risk!) and you know the credentials (or more secure even: switch to certificate based login).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文