Docker-py: permission denied

Expected behavior

I have a simple Python script using docker-py to list running containers.

import sys import docker import json import time from docker import Client cli = docker.from_env(assert_hostname=False) containers = cli.containers(quiet=True,all=True)

If I run it as root user inside the containers, it worked as expected, however, if I run it as a regular user, it gives “permission denied” error.

Question: does it require root privilege to use docker-py to access Docker daemon?

Actual behavior

File “/usr/local/lib/python2.7/dist-packages/docker/api/container.py”, line 70, in containers
res = self._result(self._get(u, params=params), True)
File “/usr/local/lib/python2.7/dist-packages/docker/utils/decorators.py”, line 47, in inner
return f(self, *args, **kwargs)
File “/usr/local/lib/python2.7/dist-packages/docker/client.py”, line 120, in _get
return self.get(url, **self._set_request_timeout(kwargs))
File “/usr/local/lib/python2.7/dist-packages/requests/sessions.py”, line 487, in get
return self.request(‘GET’, url, **kwargs)
File “/usr/local/lib/python2.7/dist-packages/requests/sessions.py”, line 475, in request
resp = self.send(prep, **send_kwargs)
File “/usr/local/lib/python2.7/dist-packages/requests/sessions.py”, line 585, in send
r = adapter.send(request, **kwargs)
File “/usr/local/lib/python2.7/dist-packages/requests/adapters.py”, line 453, in send
raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: (‘Connection aborted.’, error(13, ‘Permission denied’))

Usually, yes. (For the same reason that you need root privilege to run docker run -v /:/host ubuntu:16.04 cat /host/etc/shadow.)

1 Like

Thanks David for the clarification.

Hi i am getting the same error could you please help me sort that out ? It would be great if you can because i am stuck for a while now . Also if you followed any references, do you mind sharing them ?

Thanks a lot in advance.

I just run the Python script as root user for now.

Superuser privileges are not required to access the docker daemon with Docker for Mac. docker can be used from a regular user account (the one running the Docker for Mac app).

Inside a container that has the docker socket bind mounted into it, root may be required in order to perform operations with the docker socket.

If you find you cannot use docker with Docker for Mac without administrator privileges, check that you don’t have any DOCKER_* environment variables set (e.g. from a previous docker machine installation) which will interfere with regular function of the docker client in Docker for Mac.

Thanks. I understand that now.