Python 2.7.10,Docker 版本 18.03.1-ce-mac65 (24312)
这可能是我对 docker exec_run 命令的工作方式缺乏了解,但我正在努力让它工作。以下代码可以正常工作
from __future__ import print_function
import docker
if __name__ == '__main__':
client = docker.from_env()
image = client.images.pull('oraclelinux:7')
container = client.containers.run('oraclelinux:7',
command='ls',
stderr=True,
stdout=True,
auto_remove=False,
remove=False,
detach=True
)
log = container.logs(stdout=True, stderr=True, stream=True)
for line in log:
print(line, end='')
container.stop()
print(container.status)
container.remove()
Run Code Online (Sandbox Code Playgroud)
并返回根目录中的目录列表。但是,我希望以下内容是等效的失败。
from __future__ import print_function
import docker
if __name__ == '__main__':
client = docker.from_env()
image = client.images.pull('oraclelinux:7')
container = client.containers.create('oraclelinux:7',
command='/bin/bash',
auto_remove=False)
container.start()
log = container.exec_run('ls',
stderr=True,
stdout=True,
detach=True)
for line in log:
print(line, end='')
container.stop()
print(container.status)
container.remove()
Run Code Online (Sandbox Code Playgroud)
出现以下错误
/Users/user/PythonVirtualEnv/bin/python "/Users/user/DockerAutomation.py"
Traceback (most recent call last):
File "/Users/user/DockerAutomation.py", line 16, in <module>
detach=True)
File "/Users/user/PythonVirtualEnv/lib/python2.7/site-packages/docker/models/containers.py", line 185, in exec_run
resp['Id'], detach=detach, tty=tty, stream=stream, socket=socket
File "/Users/user/PythonVirtualEnv/lib/python2.7/site-packages/docker/utils/decorators.py", line 19, in wrapped
return f(self, resource_id, *args, **kwargs)
File "/Users/user/PythonVirtualEnv/lib/python2.7/site-packages/docker/api/exec_api.py", line 162, in exec_start
return self._result(res)
File "/Users/user/PythonVirtualEnv/lib/python2.7/site-packages/docker/api/client.py", line 231, in _result
self._raise_for_status(response)
File "/Users/user/PythonVirtualEnv/lib/python2.7/site-packages/docker/api/client.py", line 227, in _raise_for_status
raise create_api_error_from_http_exception(e)
File "/Users/user/PythonVirtualEnv/lib/python2.7/site-packages/docker/errors.py", line 31, in create_api_error_from_http_exception
raise cls(e, response=response, explanation=explanation)
docker.errors.APIError: 500 Server Error: Internal Server Error ("OCI runtime exec failed: exec failed: container_linux.go:348: starting container process caused "process_linux.go:86: executing setns process caused \"exit status 21\"": unknown")
Run Code Online (Sandbox Code Playgroud)
任何人都可以对我可能做错的事情提供任何见解吗?
编辑:虽然 Ignacio 回答了这个问题,但我添加了另一个更接近我想要做的答案(针对打开的容器运行多个命令并流式传输输出)。
感谢伊格纳西奥。我跟进了他的回答,并更接近我所追求的。我认为以下内容提供了一个更完整的解决方案,用于处理打开的容器、运行多个命令和流式传输输出。容器保持打开状态直到关闭。您需要在 bash 会话上打开 stdin,否则它认为它没有任何关系并过早关闭。
from __future__ import print_function
from time import time
import docker
if __name__ == '__main__':
client = docker.from_env()
image = client.images.pull('oraclelinux:7')
start = time()
container = client.containers.create('oraclelinux:7',
command="/bin/bash",
tty=True,
stdin_open=True,
auto_remove=False)
container.start()
print('Container Started : {}'.format(container.status))
exec_log = container.exec_run("/bin/bash -c 'for i in `seq 1 10`; do echo $i; sleep 1;done;'",
stdout=True,
stderr=True,
stream=True)
for line in exec_log[1]:
print(line, end='')
print('Container Finished outputting log : {}'.format(time() - start))
container.stop()
container.remove()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5436 次 |
| 最近记录: |