I'm trying to use python to start a process inside of existing container and communicate with it.
What I have:
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)
But when I run it I'm getting BlockingIOError: [Errno 11] Resource temporarily unavailable
Appreciate any help
Try attach_socket and set it into non-blocking mode.
params here is dict with params that you want to recv.
sock = client.attach_socket(cont, params=params)
sock._sock.setblocking(False)
After it use select in infinite loop to write/read from socket.
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 here is function with os.read/os.write
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.