"docker.errors.DockerException: Error while fetching server API version: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory'))" Error in python

I have installed docker on M1 chip in Rosatta mode and Docker daemon is also running. When I try to execute docker.from_env() in my python code I’m getting this error
“docker.errors.DockerException: Error while fetching server API version: (‘Connection aborted.’, FileNotFoundError(2, ‘No such file or directory’))”

Can someone help to resolve this issue? I tried reinstalling the docker multiple times and my docker is upto date also.

I think docker is not running or doesn’t have enough permissions.
If you are using Docker Desktop, try launching the application.
Check to see if the user running Python can use docker commands

Hello,
I have installed the same docker in M1 and I can make it work. please follow the below steps,

  1. make sure you have docker installed on the system and verify
    Screenshot 2023-04-22 at 12.46.33 PM

  2. run

docker info

expected result (redacted)

 Name: docker-desktop
 ID: 2B2V:VF6G:NA67:VXNS:HG67:NOLE:UGRL:BTCF:56EC:ZT3E:C7NU:4JET
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 HTTP Proxy: http.docker.internal:3128
 HTTPS Proxy: http.docker.internal:3128
 No Proxy: hubproxy.docker.internal
 Registry: https://index.docker.io/v1/
 Experimental: false
 Insecure Registries:
  hubproxy.docker.internal:5555
  127.0.0.0/8
 Live Restore Enabled: false
  1. below example Python code
pip install docker
import docker
client = docker.from_env()
res= client.containers.run("ubuntu", "echo hello world")
print("Running",res)
  1. Result
Running b'hello world\n'

@alishah730 I have similar setup as yours in the info but the error persists.

Hi,
Under advanced settings check the case “Allow the default Docker socket to be used (requires password)”.

5 Likes

Thank you @zandolsi, that worked.

Hi @zandolsi

I am using Docker Desktop (v 4.25.0) on MacBook M1.
My docker deamon is running and I am getting the output as expected when I run docker info in command line. In “Advanced Settings” the option of “Allow the default Docker socket to be used (requires password)” is also checked.

But when I run the python example then I am getting the following error:

docker.errors.DockerException: Error while fetching server API version: (‘Connection aborted.’, FileNotFoundError(2, ‘No such file or directory’))

Any other idea that I can solve this issue ?

if you scroll up in the exception stack trace and see:

sock.connect(self.unix_socket)
urllib3.exceptions.ProtocolError: (‘Connection aborted.’, FileNotFoundError(2, ‘No such file or directory’))

then the python sdk cannot connect to the socket it uses to communicate with docker desktop. It can be fixed by running docker context ls and setting DOCKER_HOST through something like this:

export DOCKER_HOST=unix:///home/USERNAME/.docker/desktop/docker.sock

good luck!

1 Like

Same error, setting DOCKER_HOME, setting Allow the default Docker socket to be used. Nothing works. Latest versions of everything.
OS: Sonoma 14.2
docker info
Server: Docker Desktop 4.26.1 (131620)
Engine:
Version: 24.0.7
FYI docker context ls does not work because the base install does not set PATH correctly.
export PATH=$PATH:/Applications/Docker.app/Contents/Resources/bin

PLEASE HELP!

client = docker.from_env()
Traceback (most recent call last):
File “/usr/local/lib/python3.10/dist-packages/urllib3/connectionpool.py”, line 791, in urlopen
response = self._make_request(
File “/usr/local/lib/python3.10/dist-packages/urllib3/connectionpool.py”, line 497, in _make_request
conn.request(
File “/usr/local/lib/python3.10/dist-packages/urllib3/connection.py”, line 395, in request
self.endheaders()
File “/usr/lib/python3.10/http/client.py”, line 1278, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File “/usr/lib/python3.10/http/client.py”, line 1038, in _send_output
self.send(msg)
File “/usr/lib/python3.10/http/client.py”, line 976, in send
self.connect()
File “/usr/local/lib/python3.10/dist-packages/docker/transport/unixconn.py”, line 27, 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 “/usr/local/lib/python3.10/dist-packages/requests/adapters.py”, line 486, in send
resp = conn.urlopen(
File “/usr/local/lib/python3.10/dist-packages/urllib3/connectionpool.py”, line 845, in urlopen
retries = retries.increment(
File “/usr/local/lib/python3.10/dist-packages/urllib3/util/retry.py”, line 470, in increment
raise reraise(type(error), error, _stacktrace)
File “/usr/local/lib/python3.10/dist-packages/urllib3/util/util.py”, line 38, in reraise
raise value.with_traceback(tb)
File “/usr/local/lib/python3.10/dist-packages/urllib3/connectionpool.py”, line 791, in urlopen
response = self._make_request(
File “/usr/local/lib/python3.10/dist-packages/urllib3/connectionpool.py”, line 497, in _make_request
conn.request(
File “/usr/local/lib/python3.10/dist-packages/urllib3/connection.py”, line 395, in request
self.endheaders()
File “/usr/lib/python3.10/http/client.py”, line 1278, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File “/usr/lib/python3.10/http/client.py”, line 1038, in _send_output
self.send(msg)
File “/usr/lib/python3.10/http/client.py”, line 976, in send
self.connect()
File “/usr/local/lib/python3.10/dist-packages/docker/transport/unixconn.py”, line 27, 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 “/usr/local/lib/python3.10/dist-packages/docker/api/client.py”, line 213, in _retrieve_server_version
return self.version(api_version=False)[“ApiVersion”]
File “/usr/local/lib/python3.10/dist-packages/docker/api/daemon.py”, line 181, in version
return self._result(self._get(url), json=True)
File “/usr/local/lib/python3.10/dist-packages/docker/utils/decorators.py”, line 44, in inner
return f(self, *args, **kwargs)
File “/usr/local/lib/python3.10/dist-packages/docker/api/client.py”, line 236, in _get
return self.get(url, **self._set_request_timeout(kwargs))
File “/usr/local/lib/python3.10/dist-packages/requests/sessions.py”, line 602, in get
return self.request(“GET”, url, **kwargs)
File “/usr/local/lib/python3.10/dist-packages/requests/sessions.py”, line 589, in request
resp = self.send(prep, **send_kwargs)
File “/usr/local/lib/python3.10/dist-packages/requests/sessions.py”, line 703, in send
r = adapter.send(request, **kwargs)
File “/usr/local/lib/python3.10/dist-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’))

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File “”, line 1, in
File “/usr/local/lib/python3.10/dist-packages/docker/client.py”, line 94, in from_env
return cls(
File “/usr/local/lib/python3.10/dist-packages/docker/client.py”, line 45, in init
self.api = APIClient(*args, **kwargs)
File “/usr/local/lib/python3.10/dist-packages/docker/api/client.py”, line 197, in init
self._version = self._retrieve_server_version()
File “/usr/local/lib/python3.10/dist-packages/docker/api/client.py”, line 220, in _retrieve_server_version
raise DockerException(
docker.errors.DockerException: Error while fetching server API version: (‘Connection aborted.’, FileNotFoundError(2, ‘No such file or directory’))

When I update my deocker desktop to the version 4.26.1 (131620) . the program works well.

Issue still occurs on Docker v4.30.0 on macOS v14.4.1 (23E224)

Full traceback :point_down:

Traceback (most recent call last):
  File "/home/autogen/.local/lib/python3.11/site-packages/urllib3/connectionpool.py", line 793, in urlopen
    response = self._make_request(
               ^^^^^^^^^^^^^^^^^^^
  File "/home/autogen/.local/lib/python3.11/site-packages/urllib3/connectionpool.py", line 496, in _make_request
    conn.request(
  File "/home/autogen/.local/lib/python3.11/site-packages/urllib3/connection.py", line 400, in request
    self.endheaders()
  File "/usr/local/lib/python3.11/http/client.py", line 1298, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/usr/local/lib/python3.11/http/client.py", line 1058, in _send_output
    self.send(msg)
  File "/usr/local/lib/python3.11/http/client.py", line 996, in send
    self.connect()
  File "/home/autogen/.local/lib/python3.11/site-packages/docker/transport/unixconn.py", line 27, 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/autogen/.local/lib/python3.11/site-packages/requests/adapters.py", line 486, in send
    resp = conn.urlopen(
           ^^^^^^^^^^^^^
  File "/home/autogen/.local/lib/python3.11/site-packages/urllib3/connectionpool.py", line 847, in urlopen
    retries = retries.increment(
              ^^^^^^^^^^^^^^^^^^
  File "/home/autogen/.local/lib/python3.11/site-packages/urllib3/util/retry.py", line 470, in increment
    raise reraise(type(error), error, _stacktrace)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/autogen/.local/lib/python3.11/site-packages/urllib3/util/util.py", line 38, in reraise
    raise value.with_traceback(tb)
  File "/home/autogen/.local/lib/python3.11/site-packages/urllib3/connectionpool.py", line 793, in urlopen
    response = self._make_request(
               ^^^^^^^^^^^^^^^^^^^
  File "/home/autogen/.local/lib/python3.11/site-packages/urllib3/connectionpool.py", line 496, in _make_request
    conn.request(
  File "/home/autogen/.local/lib/python3.11/site-packages/urllib3/connection.py", line 400, in request
    self.endheaders()
  File "/usr/local/lib/python3.11/http/client.py", line 1298, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/usr/local/lib/python3.11/http/client.py", line 1058, in _send_output
    self.send(msg)
  File "/usr/local/lib/python3.11/http/client.py", line 996, in send
    self.connect()
  File "/home/autogen/.local/lib/python3.11/site-packages/docker/transport/unixconn.py", line 27, 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/autogen/.local/lib/python3.11/site-packages/docker/api/client.py", line 213, in _retrieve_server_version
    return self.version(api_version=False)["ApiVersion"]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/autogen/.local/lib/python3.11/site-packages/docker/api/daemon.py", line 181, in version
    return self._result(self._get(url), json=True)
                        ^^^^^^^^^^^^^^
  File "/home/autogen/.local/lib/python3.11/site-packages/docker/utils/decorators.py", line 44, in inner
    return f(self, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/autogen/.local/lib/python3.11/site-packages/docker/api/client.py", line 236, in _get
    return self.get(url, **self._set_request_timeout(kwargs))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/autogen/.local/lib/python3.11/site-packages/requests/sessions.py", line 602, in get
    return self.request("GET", url, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/autogen/.local/lib/python3.11/site-packages/requests/sessions.py", line 589, in request
    resp = self.send(prep, **send_kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/autogen/.local/lib/python3.11/site-packages/requests/sessions.py", line 703, in send
    r = adapter.send(request, **kwargs)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/autogen/.local/lib/python3.11/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'))

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/st-agents/src/main.py", line 12, in <module>
    executor = DockerCommandLineCodeExecutor(work_dir="coding")
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/autogen/.local/lib/python3.11/site-packages/autogen/coding/docker_commandline_code_executor.py", line 88, in __init__
    client = docker.from_env()
             ^^^^^^^^^^^^^^^^^
  File "/home/autogen/.local/lib/python3.11/site-packages/docker/client.py", line 94, in from_env
    return cls(
           ^^^^
  File "/home/autogen/.local/lib/python3.11/site-packages/docker/client.py", line 45, in __init__
    self.api = APIClient(*args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/autogen/.local/lib/python3.11/site-packages/docker/api/client.py", line 197, in __init__
    self._version = self._retrieve_server_version()
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/autogen/.local/lib/python3.11/site-packages/docker/api/client.py", line 220, in _retrieve_server_version
    raise DockerException(
docker.errors.DockerException: Error while fetching server API version: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory'))

Note that:

  • Docker Desktop is open and running