Skip to content

Docker Exception: Error While Fetching Server API Version

This common Docker error occurs when the Docker client cannot communicate with the Docker daemon. The error message typically appears as:

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

Root Cause

The error indicates that Docker components cannot establish a connection to the Docker daemon. This can happen for several reasons:

  • Docker daemon is not running
  • Permission issues with the Docker socket
  • Configuration or compatibility problems
  • Docker Desktop not properly configured (on macOS/Windows)

Solutions

1. Verify Docker Daemon Status

First, check if the Docker daemon is running:

bash
# Check Docker status
systemctl status docker

# Start Docker if not running
sudo systemctl start docker

# For systems without systemd
sudo service docker start

System Compatibility

Some Linux distributions may not use systemd. If you encounter "System has not been booted with systemd as init system," use the service command instead.

2. Check Docker Socket Permissions

The Docker socket requires appropriate permissions:

bash
# Check socket permissions
ls -l /var/run/docker.sock

# Typically should show:
# srw-rw----. 1 root docker 0 Oct  4 18:04 /var/run/docker.sock

If your user doesn't have access, add yourself to the docker group:

bash
# Add user to docker group
sudo usermod -aG docker ${USER}

# Apply group changes (log out and back in, or use)
su - ${USER}

Security Note

While chmod 666 /var/run/docker.sock may work, it creates security risks by giving all users Docker access. Prefer the group method.

3. Docker Desktop Configuration (macOS/Windows)

For Docker Desktop users:

  1. Ensure Docker Desktop is running
  2. Check WSL integration settings (Windows):
    • Open Docker Desktop → Settings → Resources → WSL Integration
    • Enable integration with your WSL distribution
  3. Check socket settings (macOS):
    • Open Docker Desktop → Settings → Advanced
    • Enable "Allow the default Docker socket to be used"

4. Update to Docker Compose V2

Older docker-compose (V1) may have compatibility issues. Migrate to Docker Compose V2:

bash
# Install Docker Compose plugin
mkdir -p ~/.docker/cli-plugins/
curl -SL https://github.com/docker/compose/releases/download/v2.3.3/docker-compose-linux-x86_64 -o ~/.docker/cli-plugins/docker-compose
chmod +x ~/.docker/cli-plugins/docker-compose

# Verify installation
docker compose version
bash
sudo apt install docker-compose-v2

Use the new syntax:

bash
docker compose build  # Instead of docker-compose build
docker compose up -d  # Instead of docker-compose up -d

5. Python Dependency Issues

Recent versions of Python's requests or urllib3 libraries may cause compatibility problems:

bash
# Downgrade problematic versions
pip install 'requests==2.31.0' 'urllib3<2.0'

# Or force reinstall specific versions
pip install --force-reinstall 'requests<2.29.0' 'urllib3<2.0'

6. Check Docker Context and Environment

Verify your Docker context and environment variables:

bash
# List Docker contexts
docker context ls

# Check DOCKER_HOST environment variable
echo $DOCKER_HOST

# Default should be unix:///var/run/docker.sock

7. WSL-Specific Solutions (Windows)

For WSL2 users experiencing iptables issues:

bash
# Switch to legacy iptables
sudo update-alternatives --config iptables

# Select the legacy option (usually option 1)
# Then start Docker service
sudo service docker start

Troubleshooting Steps

Follow this systematic approach to diagnose the issue:

  1. Verify Docker is running: docker version should show both Client and Server versions
  2. Check socket connection: Ensure /var/run/docker.sock exists and is accessible
  3. Validate user permissions: Confirm your user is in the docker group
  4. Test basic Docker functionality: Run docker info to verify communication
  5. Check environment variables: Ensure DOCKER_HOST is properly set if using remote daemon

Docker Desktop vs. Docker Engine

Docker Desktop includes both the Docker Engine and convenient management tools. On Linux, you typically install Docker Engine directly, while macOS and Windows use Docker Desktop which manages the underlying VM.

Prevention

  • Regularly update Docker and Docker Compose to latest stable versions
  • Use Docker Compose V2 instead of the legacy python package
  • Avoid modifying socket permissions directly; use group membership instead
  • Keep Python dependencies pinned to compatible versions in production environments

Most cases of this error resolve by ensuring the Docker daemon is running and properly configured with correct permissions. For persistent issues, check the Docker logs for more detailed error information:

bash
# View Docker daemon logs
journalctl -u docker.service

# Or check Docker Desktop logs through the GUI

By following these solutions, you should be able to resolve the API version connection error and successfully run your Docker commands.