2

This seems simple, but I am trying to replicate the following Docker command using Docker-py:

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

Using dockerpy, it seems the following should work:

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'])

However when I run this I receive the following error trace:

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

I am not really sure what I am doing incorrectly or need to do differently for docker-py. Immediately after the above I can then execute the command line command fine.

Versions of everything:

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
enderland
  • 13,825
  • 17
  • 98
  • 152
  • 1
    This looks similar to [these](https://github.com/docker/compose/issues/1275) [bugs](https://github.com/golang/go/issues/9763) which should have been fixed. What version of Docker are you using? – morxa Mar 15 '16 at 17:26
  • @morxa added, can't believe I forgot to add that! – enderland Mar 15 '16 at 17:32
  • @JimB the error comes from the Go source code of Docker itself. I am unsure if changing the version of Go would resolve the issue or not (see [this bug in the Go github](https://github.com/golang/go/issues/9763)). – enderland Mar 15 '16 at 19:38
  • 1
    @enderland: docker is already compiled, so unless you working on the docker source, the local version of Go doesn't matter. – JimB Mar 15 '16 at 19:50
  • @JimB ah, duh. Move along here... it's Friday, err.. :( – enderland Mar 15 '16 at 19:51

1 Answers1

2

Docker-py has a bug or unexpected behavior in this situation.

The temporary solution is to instantiate a new client, which then allows you to do the following:

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 will be required until the version of Go that docker is built against includes this bug fix.

enderland
  • 13,825
  • 17
  • 98
  • 152