docker-py icon indicating copy to clipboard operation
docker-py copied to clipboard

Error calling Client.exec_start after Client.logs when using docker-machine

Open aldenpeterson-wf opened this issue 10 years ago • 6 comments

The following working code example:

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

Produces the following output, when run inside a docker-machine on Mac configuration:

 {'tls': <docker.tls.TLSConfig object at 0x101156a50>, 'base_url': 'docker.dev:2376'}

Traceback (most recent call last):
  File "/Users/aldenpeterson/go/projects/src/github.com/Workiva/Nastypath/src/python/mcve.py", line 21, in <module>
    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")

I should note that this works with containers that persist runtime and/or exit immediately. If you prefer to substitute another image that should reproduce the error.

It seems that when you run Client.logs() it prevents future Client.exec_start from working. Note that running Client.logs() on any running container will also cause this, not just the same container.

This error is rather difficult to trace down as all you see is the above obscure error (which is a Golang bug coming through Docker itself, apparently. This was fixed at some point recently - both the Golang repository and the Docker one show it as fixed, so I am not sure where the root of the bug is currently).

This does not seem to have the problem when not using docker-machine although I cannot easily verify this myself.

Here are my system details:

pip freeze | grep docker-py && python --version && docker version
You are using pip version 8.0.2, however version 8.1.0 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
docker-py==1.7.2
Python 2.7.11
Client:
 Client:
 Version:      1.10.3
 API version:  1.22
 Go version:   go1.5.3
 Git commit:   20f81dd
 Built:        Thu Mar 10 21:49:11 2016
 OS/Arch:      darwin/amd64

Server:
 Version:      1.10.3
 API version:  1.22
 Go version:   go1.5.3
 Git commit:   20f81dd
 Built:        Thu Mar 10 21:49:11 2016
 OS/Arch:      linux/amd64



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

aldenpeterson-wf avatar Mar 15 '16 20:03 aldenpeterson-wf

Has the fix made it to a release yet? There isn't anything we can do on our end about server-side issues.

shin- avatar Mar 15 '16 21:03 shin-

As I've been investigating this further, it seems like I can work around this by recreating the docker-py Client object itself (instead of reusing the same Client object for both calls). That makes me less convinced it is a bug with docker itself but more how docker-py is wrapping things.

The following does not have the problem:

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

Notice I am using two separate clients in this instance. It seems that a single Client() instances is unable to handle both logs and exec_start() without failing.

A workaround, though it feels pretty hacky, is to instantiate new Client() objects each time I need to use one.

aldenpeterson-wf avatar Mar 15 '16 21:03 aldenpeterson-wf

That makes me less convinced it is a bug with docker itself but more how docker-py is wrapping things.

No, that just means the bug is only exhibited when we're using the same session / connection to issue both commands. As far as I can tell, it's still a server bug.

Do you have a link to the issue on docker/docker?

shin- avatar Mar 15 '16 22:03 shin-

@shin- it appears closed here.

This is an open issue as is this one.

aldenpeterson-wf avatar Mar 15 '16 22:03 aldenpeterson-wf

It's a Go issue that has been fixed in 1.6. Docker 1.10.3 was compiled using Go 1.5.3 This PR upgrading Go to 1.6 has been merged in master a couple weeks ago, meaning it will be part of the next release cycle (Docker 1.11 onwards).

In the few cases where this is an issue, people are free to initiate a new connection (docker.Client) when necessary. Any solution we could implement client-side would amount to the same thing anyway.

The sane default is to have a persistent connection, for obvious reasons.

shin- avatar Mar 15 '16 22:03 shin-

@shin- I did not see that PR for Docker when I looked before.

This can probably be closed then, though hopefully anyone else that has this same problem can benefit from my efforts.

aldenpeterson-wf avatar Mar 16 '16 13:03 aldenpeterson-wf