docker.errors.DockerException: Error while fetching server API version
Fixes docker-py failing because the Docker daemon is unreachable. Use when from_env() or any client call raises this on connect. Not for import errors.
TL;DR: The client cannot talk to the Docker daemon: it is not running, or you lack permission to reach the socket. Start the daemon (or Docker Desktop) and make sure your user can read the socket.
docker.errors.DockerException: Error while fetching server API version: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory'))Fix it
- Check the daemon: docker version. Expected: both Client and Server sections. If Server errors, the daemon is down.
- Start it: systemctl start docker on Linux, or launch Docker Desktop on Mac/Windows. Expected: docker version shows the server.
- If the daemon runs but you get permission denied, add your user to the docker group and re-login: sudo usermod -aG docker $USER. Expected: docker ps works without sudo.
- If you use a remote daemon, set DOCKER_HOST to the right tcp or ssh URL. Expected: the client connects.
When this applies
- Any docker-py call fails with Error while fetching server API version.
When it doesn't
- docker version works in the shell but Python fails: check DOCKER_HOST env in the Python process.
- The error is APIError 400/404: the daemon is reachable; the request is bad.
Compatibility
- docker-py any version; daemon any recent.
Why it happens
docker-py probes the daemon's /version endpoint on first use. No daemon, no socket, or no permission all surface as this one error.
Edge cases
- In CI, use the docker:dind service or mount the host socket; a bare python image has no daemon.
- Snap-installed docker uses a different socket path; set DOCKER_HOST accordingly.