Search code examples
pythondockercontainersdocker-desktopdockerpy

Docker python client cannot connect docker desktop


I'm using docker-desktop on ubuntu18.04 and I run some containers successfully with it. I try to use docker python client as client = docker.from_env() but it failed as follows:

Traceback (most recent call last):
  File "/home/caohch1/anaconda3/envs/mointor/lib/python3.10/site-packages/urllib3/connectionpool.py", line 714, in urlopen
    httplib_response = self._make_request(
  File "/home/caohch1/anaconda3/envs/mointor/lib/python3.10/site-packages/urllib3/connectionpool.py", line 415, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/home/caohch1/anaconda3/envs/mointor/lib/python3.10/http/client.py", line 1283, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/home/caohch1/anaconda3/envs/mointor/lib/python3.10/http/client.py", line 1329, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/home/caohch1/anaconda3/envs/mointor/lib/python3.10/http/client.py", line 1278, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/home/caohch1/anaconda3/envs/mointor/lib/python3.10/http/client.py", line 1038, in _send_output
    self.send(msg)
  File "/home/caohch1/anaconda3/envs/mointor/lib/python3.10/http/client.py", line 976, in send
    self.connect()
  File "/home/caohch1/anaconda3/envs/mointor/lib/python3.10/site-packages/docker/transport/unixconn.py", line 43, in connect
    sock.connect(self.unix_socket)
FileNotFoundError: [Errno 2] No such file or directory

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/caohch1/anaconda3/envs/mointor/lib/python3.10/site-packages/requests/adapters.py", line 486, in send
    resp = conn.urlopen(
  File "/home/caohch1/anaconda3/envs/mointor/lib/python3.10/site-packages/urllib3/connectionpool.py", line 798, in urlopen
    retries = retries.increment(
  File "/home/caohch1/anaconda3/envs/mointor/lib/python3.10/site-packages/urllib3/util/retry.py", line 550, in increment
    raise six.reraise(type(error), error, _stacktrace)
  File "/home/caohch1/anaconda3/envs/mointor/lib/python3.10/site-packages/urllib3/packages/six.py", line 769, in reraise
    raise value.with_traceback(tb)
  File "/home/caohch1/anaconda3/envs/mointor/lib/python3.10/site-packages/urllib3/connectionpool.py", line 714, in urlopen
    httplib_response = self._make_request(
  File "/home/caohch1/anaconda3/envs/mointor/lib/python3.10/site-packages/urllib3/connectionpool.py", line 415, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/home/caohch1/anaconda3/envs/mointor/lib/python3.10/http/client.py", line 1283, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/home/caohch1/anaconda3/envs/mointor/lib/python3.10/http/client.py", line 1329, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/home/caohch1/anaconda3/envs/mointor/lib/python3.10/http/client.py", line 1278, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/home/caohch1/anaconda3/envs/mointor/lib/python3.10/http/client.py", line 1038, in _send_output
    self.send(msg)
  File "/home/caohch1/anaconda3/envs/mointor/lib/python3.10/http/client.py", line 976, in send
    self.connect()
  File "/home/caohch1/anaconda3/envs/mointor/lib/python3.10/site-packages/docker/transport/unixconn.py", line 43, in connect
    sock.connect(self.unix_socket)
urllib3.exceptions.ProtocolError: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/caohch1/anaconda3/envs/mointor/lib/python3.10/site-packages/docker/api/client.py", line 214, in _retrieve_server_version
    return self.version(api_version=False)["ApiVersion"]
  File "/home/caohch1/anaconda3/envs/mointor/lib/python3.10/site-packages/docker/api/daemon.py", line 181, in version
    return self._result(self._get(url), json=True)
  File "/home/caohch1/anaconda3/envs/mointor/lib/python3.10/site-packages/docker/utils/decorators.py", line 46, in inner
    return f(self, *args, **kwargs)
  File "/home/caohch1/anaconda3/envs/mointor/lib/python3.10/site-packages/docker/api/client.py", line 237, in _get
    return self.get(url, **self._set_request_timeout(kwargs))
  File "/home/caohch1/anaconda3/envs/mointor/lib/python3.10/site-packages/requests/sessions.py", line 602, in get
    return self.request("GET", url, **kwargs)
  File "/home/caohch1/anaconda3/envs/mointor/lib/python3.10/site-packages/requests/sessions.py", line 589, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/caohch1/anaconda3/envs/mointor/lib/python3.10/site-packages/requests/sessions.py", line 703, in send
    r = adapter.send(request, **kwargs)
  File "/home/caohch1/anaconda3/envs/mointor/lib/python3.10/site-packages/requests/adapters.py", line 501, in send
    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/caohch1/Desktop/monitor/log_extractor.py", line 2, in <module>
    client = docker.from_env()
  File "/home/caohch1/anaconda3/envs/mointor/lib/python3.10/site-packages/docker/client.py", line 96, in from_env
    return cls(
  File "/home/caohch1/anaconda3/envs/mointor/lib/python3.10/site-packages/docker/client.py", line 45, in __init__
    self.api = APIClient(*args, **kwargs)
  File "/home/caohch1/anaconda3/envs/mointor/lib/python3.10/site-packages/docker/api/client.py", line 197, in __init__
    self._version = self._retrieve_server_version()
  File "/home/caohch1/anaconda3/envs/mointor/lib/python3.10/site-packages/docker/api/client.py", line 221, in _retrieve_server_version
    raise DockerException(
docker.errors.DockerException: Error while fetching server API version: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory'))

I browsed the related solutions and found that my Unit docker.service seems not running. I also failed to start it.

caohch1@caohch1-OptiPlex-7090:~/Desktop/monitor$ systemctl status docker
Unit docker.service could not be found.
caohch1@caohch1-OptiPlex-7090:~/Desktop/monitor$ systemctl start docker
Failed to start docker.service: Unit docker.service not found.
caohch1@caohch1-OptiPlex-7090:~/Desktop/monitor$ sudo snap status docker
error: unknown command "status", see 'snap help'.
caohch1@caohch1-OptiPlex-7090:~/Desktop/monitor$ sudo snap start docker
error: snap "docker" not found

However, I'm sure that my docker desktop works well and all containers are running normally.

I'm not sure whether it is caused by docker-desktop. How can I make the docker python client connect to my docker?


Solution

  • Set the $DOCKER_HOST solve this problem. As mentioned in docker-py issue#3059:

    With latest release of docker, the context of the client is changed from default to desktop-linux which uses different endpoint and therefore breaks the docker client.

    Thus, execute following commands to set the correct $DOCKER_HOST can make docker python client works normally.

    (mointor) caohch1@caohch1-OptiPlex-7090:~/Desktop/monitor$ docker info
    Client:
     Context:    desktop-linux
    ...
    
    (mointor) caohch1@caohch1-OptiPlex-7090:~/Desktop/monitor$ docker context ls
    NAME                TYPE                DESCRIPTION                               DOCKER ENDPOINT                                    KUBERNETES ENDPOINT   ORCHESTRATOR
    default             moby                Current DOCKER_HOST based configuration   unix:///var/run/docker.sock                                              
    desktop-linux *     moby                Docker Desktop                            unix:///home/caohch1/.docker/desktop/docker.sock 
    
    (mointor) caohch1@caohch1-OptiPlex-7090:~/Desktop/monitor$ export DOCKER_HOST="unix:///home/caohch1/.docker/desktop/docker.sock"