docker.from_env() fails in Python while the docker CLI works
Your error
docker.errors.DockerException: Error while fetching server API version: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory'))3 more lines it can showFix it fast
Most common cause, 1 of 3The CLI talks to a context socket the SDK does not read
Upgrade the SDK: since 7.2.0, docker.from_env() follows the active CLI context when DOCKER_HOST is unset.
pip install --upgrade "docker>=7.2.0"
# or, on an older SDK, copy the endpoint of the active context:
export DOCKER_HOST="$(docker context inspect desktop-linux --format '{{.Endpoints.docker.Host}}')"Short answer
docker.errors.DockerException: Error while fetching server API version means the Python Docker SDK failed on its very first call to the daemon. If the docker CLI works in the same shell, the SDK is older than 7.2.0 and ignores your CLI context, or older than 7.1.0 and broken by requests 2.32. Upgrade the docker package to 7.2.0 or set DOCKER_HOST.
Which cause do you have?
In the shell and virtual environment that runs your script, ask the CLI which context it uses and list the installed library versions. If docker version also fails, the daemon itself is down or blocked, which is a different problem from the SDK cases below.
docker context ls
docker version --format '{{.Server.Version}}'
pip show docker requests urllib3 | grep -E "^(Name|Version)"Then find the line you see in this list and open its fix:
- 1The CLI talks to a context socket the SDK does not read
The error ends in FileNotFoundError, docker version works, and the starred context in docker context ls is not unix:///var/run/docker.sock - 2requests 2.32 or newer with docker-py older than 7.1.0
The message says Not supported URL scheme http+docker, and pip show lists docker 7.0.x or 6.x - 3requests 2.29+ or urllib3 2 with docker-py older than 6.1.0
The message mentions an unexpected keyword argument 'chunked', and docker is 6.0.x or older
What does the error look like?
docker.errors.DockerException: Error while fetching server API version: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory'))docker.errors.DockerException: Error while fetching server API version: Not supported URL scheme http+dockerurllib3.exceptions.URLSchemeUnknown: Not supported URL scheme http+dockerdocker.errors.DockerException: Error while fetching server API version: request() got an unexpected keyword argument 'chunked'
Cause 1 of 3
The CLI talks to a context socket the SDK does not read
You seeThe error ends in FileNotFoundError, docker version works, and the starred context in docker context ls is not unix:///var/run/docker.sock
Check
With FileNotFoundError in the DockerException text, the SDK looked for a socket file that does not exist. Docker Desktop and Colima, for example, put the socket under your home directory and point the CLI at it through a context. docker-py before 7.2.0 builds its client only from DOCKER_HOST and falls back to /var/run/docker.sock, so it never sees that context.
docker context ls
ls -l /var/run/docker.sockFix
Upgrade the SDK: since 7.2.0, docker.from_env() follows the active CLI context when DOCKER_HOST is unset. If you must stay on an older release, export DOCKER_HOST with the endpoint of the starred context before Python starts.
pip install --upgrade "docker>=7.2.0"
# or, on an older SDK, copy the endpoint of the active context:
export DOCKER_HOST="$(docker context inspect desktop-linux --format '{{.Endpoints.docker.Host}}')"Verify
The SDK prints the daemon's API version instead of raising DockerException.
python -c "import docker; print(docker.from_env().version()['ApiVersion'])"Confirmed by raw.githubusercontent.com
Cause 2 of 3
requests 2.32 or newer with docker-py older than 7.1.0
You seeThe message says Not supported URL scheme http+docker, and pip show lists docker 7.0.x or 6.x
Check
A Not supported URL scheme http+docker failure started with requests 2.32.0, which broke every docker-py release before 7.1.0; the maintainers shipped the fix in 7.1.0. Any environment that pulls a fresh requests but keeps an old docker pin still hits it.
pip show docker requests | grep -E "^(Name|Version)"Fix
Raise the docker pin to 7.1.0 or later, ideally the current 7.2.0. Holding requests at 2.31 also makes the message go away, but it keeps the outdated SDK and only moves the problem to the next upgrade.
pip install --upgrade "docker>=7.2.0"
pip freeze | grep -E "^(docker|requests)=="Verify
docker.from_env() returns a client and the http+docker message is gone.
python -c "import docker; print(docker.from_env().ping())"Confirmed by github.com
Cause 3 of 3
requests 2.29+ or urllib3 2 with docker-py older than 6.1.0
You seeThe message mentions an unexpected keyword argument 'chunked', and docker is 6.0.x or older
Check
The chunked keyword error belongs to docker-py releases from before 6.1.0. A feature added in requests 2.29.0 changed how those releases' own Unix socket adapters get called, and urllib3 2 arrived at the same time; 6.1.0 added support for both. Ansible's community.docker collection carries its own copy of the SDK and needed 3.4.5 for the same fix.
pip show docker requests urllib3 | grep -E "^(Name|Version)"Fix
Upgrade docker to the current release instead of pinning requests or urllib3 to old versions, and rebuild any image or lock file that still holds the old SDK. For Ansible, upgrade the community.docker collection.
pip install --upgrade "docker>=7.2.0"
ansible-galaxy collection install community.docker --upgradeVerify
The client connects and ping() returns True.
python -c "import docker; print(docker.from_env().ping())"Confirmed by github.com
How do I stop it coming back?
- Pin docker>=7.2.0 in requirements files, so a fresh requests or urllib3 never meets an SDK that predates them.
- In CI images that run Python tests against Docker, print docker context ls and the docker package version at the start of the job.
- Set DOCKER_HOST explicitly in services and cron jobs, where no user shell or CLI context is loaded.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine on the same stack: TaxiDex, Dispatch Pro, stegnor.com.