Error retrieving Docker server API version during docker-compose build

I am facing an issue when trying to build my application with docker-compose. Each time I execute the build command, I receive an error related to fetching the server API version.

git clone https://github.com/myproject/myapp.git
cd myapp
docker-compose build

The error message displayed is:

Traceback (most recent call last):
  File "/home/user/.local/bin/docker-compose", line 10, in <module>
    sys.exit(main())
  File "/home/user/.local/lib/python3.8/site-packages/compose/cli/main.py", line 75, in main
    execute_command()
  File "/home/user/.local/lib/python3.8/site-packages/compose/cli/main.py", line 135, in execute_command
    app = create_project('.', options)
  File "/home/user/.local/lib/python3.8/site-packages/compose/cli/command.py", line 68, in create_project
    return initialize_project(
  File "/home/user/.local/lib/python3.8/site-packages/compose/cli/command.py", line 145, in initialize_project
    docker_client = create_client(
  File "/home/user/.local/lib/python3.8/site-packages/compose/cli/docker_client.py", line 48, in create_client
    docker_client = get_docker_client(
  File "/home/user/.local/lib/python3.8/site-packages/compose/cli/docker_client.py", line 185, in get_docker_client
    docker_client = APIClient(**params)
  File "/home/user/.local/lib/python3.8/site-packages/docker/api/client.py", line 195, in __init__
    self._api_version = self._get_server_version()
  File "/home/user/.local/lib/python3.8/site-packages/docker/api/client.py", line 225, in _get_server_version
    raise DockerException(
docker.errors.DockerException: Error while fetching server API version: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory'))

Update:

I attempted to manually start the Docker service, but I still cannot connect:

user@machine:~$ sudo service docker start
[sudo] password for user:
 * Starting Docker: docker                                               [ OK ]

user@machine:~$ docker --version
Docker version 19.03.13, build 4484c46d9d

user@machine:~$ docker info
Client:
 Debug Mode: false

Server:
ERROR: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

user@machine:~$ sudo service docker restart
 * Stopping Docker: docker                                               [ OK ]
 * Starting Docker: docker                                               [ OK ]

It seems the daemon starts correctly, but I am still unable to make a connection. Has anyone else experienced this problem?

Had this exact issue on Ubuntu 18.04 last month. Docker service looked fine but the daemon wasn’t actually running. Check the daemon logs first: journalctl -u docker.service -f - that’s what showed me the real problem. Turned out my docker.json config file was corrupted. You can also try starting the daemon manually with sudo dockerd to catch startup errors that the service command misses. The daemon sometimes fails silently but the service still says everything’s good. If you see permission errors in the logs, you might need to reset ownership on the whole /var/lib/docker directory.

This is definitely a socket permission issue. The service starts but the daemon isn’t binding to the socket properly. Check if the socket file exists: ls -la /var/run/docker.sock and look at the permissions. The socket often gets created with wrong ownership. Also run sudo systemctl status docker - you’ll probably see error messages that the service command missed. If basic troubleshooting doesn’t work, I’d completely purge and reinstall docker-ce. That usually fixes these stubborn daemon connection problems.

hey, you might wanna try adding ur user to the docker group: sudo usermod -aG docker $USER. after that, log out and log back in. it worked for me when i had a similar issue!