[英]Create and communicate with process inside container
我正在尝试使用python在现有容器内启动进程并与之通信。
我有的:
import docker
import os
client = docker.APIClient()
buf = b"ls"
exec_setup = client.exec_create(container="some-tag", cmd="/bin/bash", stdin=True, tty=True)
socket = client.exec_start(exec_id = exec_setup["Id"], socket=True)
written = os.write(socket.fileno(), buf)
nxt = os.read(socket.fileno(), 1024)
print(nxt)
但是,当我运行它时,我得到BlockingIOError:[Errno 11]资源暂时不可用
感谢任何帮助
尝试attach_socket并将其设置为非阻止模式。
这里的params是带有要恢复的paras的dict。
sock = client.attach_socket(cont, params=params)
sock._sock.setblocking(False)
之后,在无限循环中使用select来从套接字写入/读取。
buff = b''
while True:
read, write, _ = select.select([sock], [sock], [], 1)
if read:
try:
data = socket_read(sock)
except Exception:
break
if data is None:
break
stream_data += data
if write and stdin:
try:
written = socket_write(sock, stdin)
except Exception:
break
stdin = stdin[written:]
if not stdin:
sock._sock.shutdown(socket.SHUT_WR)
socket_read / socket_write这是os.read/os.write的功能
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.