Search code examples
macospython-2.7dockerdockerpy

Dockerpy executing command to cat log file


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

Solution

  • 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.