Docker-py: ConnectionError: ('Connection aborted.', error(2, 'No such file or directory'))

Created on 5 Dec 2016  路  13Comments  路  Source: docker/docker-py

Attempting to spawn a docker container from within a docker container using docker-py.

OS X, dockerpy-1.10.6

Here is my code (select_device is a simple, known-good function)

docker_client = docker.from_env()
worker = docker_client.create_container("worker",
                                    environment={'DEVICE':select_device(priority),
                                        'GIT_REPO':git_repo, 'TASK_ID':delegate.request.id})

Here is the trace:

delegation_1  | [2016-12-05 07:39:41,079: INFO/MainProcess] Received task: delegate[e28262d3-1862-456c-ba43-013114fc7dbf]
delegation_1  | [2016-12-05 07:39:43,095: ERROR/PoolWorker-3] Task delegate[e28262d3-1862-456c-ba43-013114fc7dbf] raised unexpected: ConnectionError(ProtocolError('Connection aborted.', error(2, 'No such file or directory')),)
delegation_1  | Traceback (most recent call last):
delegation_1  |   File "/usr/local/lib/python2.7/dist-packages/celery/app/trace.py", line 368, in trace_task
delegation_1  |     R = retval = fun(*args, **kwargs)
delegation_1  |   File "/usr/local/lib/python2.7/dist-packages/celery/app/trace.py", line 623, in __protected_call__
delegation_1  |     return self.run(*args, **kwargs)
delegation_1  |   File "/opt/app/delegate.py", line 45, in delegate
delegation_1  |     'GIT_REPO':git_repo, 'TASK_ID':delegate.request.id})
delegation_1  |   File "/usr/local/lib/python2.7/dist-packages/docker/api/container.py", line 135, in create_container
delegation_1  |     return self.create_container_from_config(config, name)
delegation_1  |   File "/usr/local/lib/python2.7/dist-packages/docker/api/container.py", line 145, in create_container_from_config
delegation_1  |     res = self._post_json(u, data=config, params=params)
delegation_1  |   File "/usr/local/lib/python2.7/dist-packages/docker/client.py", line 198, in _post_json
delegation_1  |     return self._post(url, data=json.dumps(data2), **kwargs)
delegation_1  |   File "/usr/local/lib/python2.7/dist-packages/docker/utils/decorators.py", line 47, in inner
delegation_1  |     return f(self, *args, **kwargs)
delegation_1  |   File "/usr/local/lib/python2.7/dist-packages/docker/client.py", line 135, in _post
delegation_1  |     return self.post(url, **self._set_request_timeout(kwargs))
delegation_1  |   File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 535, in post
delegation_1  |     return self.request('POST', url, data=data, json=json, **kwargs)
delegation_1  |   File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 488, in request
delegation_1  |     resp = self.send(prep, **send_kwargs)
delegation_1  |   File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 609, in send
delegation_1  |     r = adapter.send(request, **kwargs)
delegation_1  |   File "/usr/local/lib/python2.7/dist-packages/requests/adapters.py", line 473, in send
delegation_1  |     raise ConnectionError(err, request=request)
delegation_1  | ConnectionError: ('Connection aborted.', error(2, 'No such file or directory'))

Am I crazy in thinking I can run docker-py from within a docker container?

Most helpful comment

Am I crazy in thinking

No, you're not. But you have to bind-mount docker socket (if that's how you access docker daemon) inside the container. Then you're fine.

The error here means that /run/docker.sock doesn't exist.

All 13 comments

Am I crazy in thinking

No, you're not. But you have to bind-mount docker socket (if that's how you access docker daemon) inside the container. Then you're fine.

The error here means that /run/docker.sock doesn't exist.

@TomasTomecek

could I get some guidance on what that would look like from docker-py?

I imagine it's something akin to docker_client = Client('/var/run/docker.sock')?

Or would it be from the Dockerfile of the container doing the spawning?

EDIT: It's from the Dockerfile Volumes:/var/run/docker.sock:/var/run/docker.sock

I tried adding this mount but it did not help - still getting the same error

Dockerfiles are a build time thing: no mounts are preserved in runtime.

You need to create and start the container with the mounts. Please see the API.

I did not edit the Dockerfile, what I did was to mount the container /var/run dir to the same host dir, but it did not help...

Hey guys,

I tried to mount volume for docker.sock and i got this error:
requests.exceptions.ConnectionError: ('Connection aborted.', error(111, 'Connection refused'))

Instead No such file or directory now i'm getting Connection refused
Perhaps it's a permission problem, someone got this problem after mounted volume?

Any update on here ? What was the solution ?

Any update on here ? What was the solution ?

I'm sorry Leo.
I'm pretty sure that I resolved but I don't know if I followed the same approach.

Try execute with this parameter: privileged=True

I added /var/run/docker.sock:/var/run/docker.sock but now I get PermissionError: [Errno 13] Permission denied but I am not sure if it is a web oauth issue, or if the /var/run/docker.sock doesnt have the right user permission ....

I added /var/run/docker.sock:/var/run/docker.sock but now I get PermissionError: [Errno 13] Permission denied but I am not sure if it is a web oauth issue, or if the /var/run/docker.sock doesnt have the right user permission ....

Try using privileged=True flag. Should work.

I did, but still not working. I also changed the permission on the sock but i am still getting new errors. I am trying to pass --conn_extra "{\"reauth\": false} when I create the connection. Following what the code is doing seem that it might solve the problem but now I am getting KeyError: '"reauth"'.

If you get No such file or directory, check the docker service is running: sudo systemctl start docker

If you get Permission denied, make sure your user is added into the "docker" group: sudo usermod -aG docker ${USER}

If you get No such file or directory, check the docker service is running: sudo systemctl start docker

If you get Permission denied, make sure your user is added into the "docker" group: sudo usermod -aG docker ${USER}

This works fine THX

Was this page helpful?
0 / 5 - 0 ratings