簡體   English   中英

在容器內創建過程並與之進行通信

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM