Python Docker SDK docker exec_run在运行时失败 [英] Python Docker SDK docker exec_run fails whilst run works

查看:178
本文介绍了Python Docker SDK docker exec_run在运行时失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python 2.7.10,Docker版本18.03.1-ce-mac65(24312)

Python 2.7.10, Docker Version 18.03.1-ce-mac65 (24312)

我可能对docker exec_run命令的工作方式缺乏了解,但是我正在努力使其工作.以下代码可以正常工作

This may be a lack of understanding on my part as to how the docker exec_run command works but I'm struggling to get it to work. The following code works without issue

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()

并返回根目录中的目录列表.但是,我原希望与之等效的以下方法却失败了.

And returns a list of directories in the root directory. However the following which I had hoped would be equivalent fails.

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()

出现以下错误

/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")

任何人都可以对我可能在做错的事情提供任何见解吗?

Can anyone offer any insight into what I might be doing wrong?

在伊格纳西奥(Ignacio)回答问题的同时,我添加了另一个答案,该答案更接近于我想做的事情(对一个打开的容器运行多个命令并输出输出).

Edit : Whilst Ignacio answered the question, I've added another answer which comes closer to what I wanted to do (run multiple commands against an open container and stream the output).

推荐答案

我认为问题出在将其设置为true时,将执行命令,但Docker客户端未附加到输出.

With it set to true, command is executed but the docker client doesn't attach to the outputs.

我在测试时发现了另一个问题,使用/bin/bash命令运行容器会导致容器在到达exec_run之前就结束了.

I found another issue while testing, running the container with the command /bin/bash cause the container to end before even reaching 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.create('oraclelinux:7',
                                         command='sleep 10000',
                                         auto_remove=False)
    container.start()

    log = container.exec_run('ls',
                             stderr=True,
                             stdout=True)

    for line in log:
        print(line, end='')

    container.stop()
    print(container.status)
    container.remove()

这篇关于Python Docker SDK docker exec_run在运行时失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆