如何使用 docker-py 将伪 tty 附加到 Docker 容器以复制`docker exec -ti <container> <command>` 的行为?

问题描述

我使用的是最新版本的 Docker Py,但我无法将伪 tty 附加到已运行的容器以确保复制 docker exec -ti <container> <command> 的行为。任何帮助将不胜感激。

解决方法

尝试使用 docker-py 中的 attach 方法或 attach-stream

如 docker-py attach 中所述,该方法将 tty(s) 附加到正在运行的容器。这类似于原生 docker attach command 将 stdin、stdout 和 stderr 附加到容器。

在调用 stdin_open = True 时需要使用 tty = truecreate_container 创建容器以使 attach 工作。

使用 attach-socket 的示例:

拉动 debian:latest 容器

docker pull debian:latest

创建python脚本test.py如下

#! python3
import docker
import os
import time

# Create container and start it
client = docker.from_env()
container = client.create_container('debian:latest',name='test',stdin_open = True,tty = True,command = 'sh')
client.start(container)

# Create communication socket
s = client.attach_socket(container,{'stdin': 1,'stdout': 1,'stream':1})

# Set the socket as non-blocking
s._sock.setblocking(False)

# Start communication by sending cat
os.write(s.fileno(),b'cat /etc/hosts\n')

# Since we are non-blocking,wait for a while to get the output
time.sleep(1)

# Read up-to 10000 bytes. If there are more,we can issue another read
print(os.read(s.fileno(),10000))

client.stop(container)
client.wait(container)
client.remove_container(container)

现在测试脚本。您将看到我们在 shell 中执行的命令的输出。

~# python3 test.py
b'cat /etc/hosts\r\n127.0.0.1\tlocalhost\r\n::1\tlocalhost ip6-localhost ip6-loopback\r\nfe00::0\tip6-localnet\r\nff00::0\tip6-mcastprefix\r\nff02::1\tip6-allnodes\r\nff02::2\tip6-allrouters\r\n172.17.0.2\t48f4a5f32f48\r\n# '

注意:socket 设置为 non-blockin 是为了避免在等待容器响应时被阻塞。这种方法特定于用例。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...