Dockerpy执行命令到cat日志文件

时间:2016-03-15 16:52:22

标签: macos python-2.7 docker dockerpy

这看起来很简单,但我试图使用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

1 个答案:

答案 0 :(得分: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'])

在构建针对this bug fix包含the create docs的.dock版本之前,这将是必需的。