Dockerpy 对 cat 日志文件执行命令

Dockerpy executing command to cat log file

这看起来很简单,但我正在尝试使用 Docker-py 复制以下 Docker 命令:

docker exec dockerName cat /var/log/foo.log

使用 dockerpy,似乎以下应该有效:

from docker.client import Client
from docker.utils import kwargs_from_env
cli = Client(**kwargs_from_env())

print kwargs_from_env()

name_one = 'exec_container'
cli.create_container(**{'name': name_one, 'image': 'golang'})
cli.start(name_one)
cli.logs(name_one, stdout=True, stderr=True)  # commenting this line out allows the below to execute perfectly

e = cli.exec_create(container=name_one, cmd='ls /usr/local/bin')
print cli.exec_start(exec_id=e['Id'])

然而,当我 运行 这样做时,我收到以下错误跟踪:

  File "/Library/Python/2.7/site-packages/docker/utils/decorators.py", line 35, in wrapper
    return f(self, *args, **kwargs)
  File "/Library/Python/2.7/site-packages/docker/api/exec_api.py", line 75, in exec_start
    return self._get_result_tty(stream, res, tty)
  File "/Library/Python/2.7/site-packages/docker/client.py", line 311, in _get_result_tty
    self._raise_for_status(res)
  File "/Library/Python/2.7/site-packages/docker/client.py", line 146, in _raise_for_status
    raise errors.APIError(e, response, explanation=explanation)
docker.errors.APIError: 500 Server Error: Internal Server Error ("http: Hijack is incompatible with use of CloseNotifier")

我不太确定我做错了什么或需要为 docker-py 做不同的事情。在上面我可以立即执行命令行命令。

所有版本:

docker --version
Docker version 1.10.0, build 590d5108

docker-machine --version
docker-machine version 0.6.0, build e27fb87

pip freeze | grep docker-py
docker-py==1.7.2

Docker-py 有一个 bug or unexpected behavior in this situation.

临时解决方案是实例化一个新客户端,然后您可以执行以下操作:

from docker.client import Client
from docker.utils import kwargs_from_env
cli = Client(**kwargs_from_env())

print kwargs_from_env()
name_one = 'exec_container'

cli.create_container(**{'name': name_one, 'image': 'golang'})
cli.start(name_one)
cli.logs(name_one, stdout=True, stderr=True)

cli2 = Client(**kwargs_from_env())

e = cli2.exec_create(container=name_one, cmd='ls /usr/local/bin')
print cli2.exec_start(exec_id=e['Id'])

在构建 docker 所针对的 Go 版本包括 this bug fix 之前,这将是必需的。