docker.errors.DockerException: Error while fetching server API version

172,623

Solution 1

Are you sure docker is running on your system? You can get that error when compose is not able to connect to docker via docker socket (if any other way for connection is not defined).

If you are running on linux, usually you can run systemctl status docker to check if docker daemon is running and systemctl start docker to start it.

It would help to tell what OS and docker version are you using.

Solution 2

set the permission like this,

sudo chmod 666 /var/run/docker.sock

Solution 3

When used WSL (Windows Subsystem for Linux) need to enable 'WSL Integration' for required distro in Windows Docker Desktop (Settings -> Resources-> WSL Integration -> Enable integration with required distros).

WSL Integration

Solution 4

sudo service docker start or
sudo service docker restart

Solution 5

By default, the docker command can only be run the root user or by a user in the docker group, which is automatically created during Docker’s installation process. If you want to avoid typing sudo whenever you run the docker command, add your username to the docker group:

sudo usermod -aG docker ${USER}

To apply the new group membership, log out of the server and back in, or type the following:

su - ${USER}

You will be prompted to enter your user’s password to continue.

Share:
172,623

Related videos on Youtube

ming
Author by

ming

Updated on April 10, 2022

Comments

  • ming
    ming about 2 years

    I want to install this module but there is something wrong when I try the step docker-compose build ...

    I tried to update the Docker version and restart Docker many times. But it didn't work.

    git clone https://github.com/uhh-lt/158.git
    cd 158
    docker-compose build
    
    File "/home/ming/.local/bin/docker-compose", line 8, in <module>
        sys.exit(main())
      File "/home/ming/.local/lib/python3.8/site-packages/compose/cli/main.py", line 67, in main
        command()
      File "/home/ming/.local/lib/python3.8/site-packages/compose/cli/main.py", line 123, in perform_command
        project = project_from_options('.', options)
      File "/home/ming/.local/lib/python3.8/site-packages/compose/cli/command.py", line 60, in project_from_options
        return get_project(
      File "/home/ming/.local/lib/python3.8/site-packages/compose/cli/command.py", line 131, in get_project
        client = get_client(
      File "/home/ming/.local/lib/python3.8/site-packages/compose/cli/docker_client.py", line 41, in get_client
        client = docker_client(
      File "/home/ming/.local/lib/python3.8/site-packages/compose/cli/docker_client.py", line 170, in docker_client
        client = APIClient(**kwargs)
      File "/home/ming/.local/lib/python3.8/site-packages/docker/api/client.py", line 188, in __init__
        self._version = self._retrieve_server_version()
      File "/home/ming/.local/lib/python3.8/site-packages/docker/api/client.py", line 212, in _retrieve_server_version
        raise DockerException(
    docker.errors.DockerException: Error while fetching server API version: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory'))
    

    Update 2020-11-23

    Thanks for two of you helping me with the error! I tried the commend but can't connect to my Docker:

    ming@KITM-7664:~$ sudo /etc/init.d/docker start
    [sudo] password for ming:
     * Starting Docker: docker                                                                                       [ OK ]
    
    ming@KITM-7664:~$ which docker
    /usr/bin/docker
    
    ming@KITM-7664:~$ docker version
    Client: Docker Engine - Community
     Version:           19.03.13
     API version:       1.40
     Go version:        go1.13.15
     Git commit:        4484c46d9d
     Built:             Wed Sep 16 17:02:52 2020
     OS/Arch:           linux/amd64
     Experimental:      false
    Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
    
    ming@KITM-7664:~$ systemctl status docker
    System has not been booted with systemd as init system (PID 1). Can't operate.
    Failed to connect to bus: Host is down
    
    ming@KITM-7664:~$ systemctl start docker
    System has not been booted with systemd as init system (PID 1). Can't operate.
    Failed to connect to bus: Host is down
    
    ming@KITM-7664:~$ sudo /etc/init.d/docker start
     * Starting Docker: docker                                                                                       [ OK ]
    
    ming@KITM-7664:~$ docker version
    Client: Docker Engine - Community
     Version:           19.03.13
     API version:       1.40
     Go version:        go1.13.15
     Git commit:        4484c46d9d
     Built:             Wed Sep 16 17:02:52 2020
     OS/Arch:           linux/amd64
     Experimental:      false
    Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
    
    • YJDev
      YJDev about 3 years
      make sure that docker desktop is up running. In my case I got docker.errors.DockerException: Error while fetching server API version: (2, 'CreateFile', 'The system cannot find the file specified.') because I manually stopped docker desktop. Once I restarted it, everthing worked as expected.
    • Hanna
      Hanna over 2 years
      @mafei that made the trick in my case! Thank you! :)
    • Dash2TheDot
      Dash2TheDot almost 2 years
      In addition to What @YJDev said, I installed docker-desktop, which I guess takes over starting the docker service. So once I started up docker-desktop, docker-compose ran like a charm!
  • ming
    ming over 3 years
    Thank you for the help!! I'm using ubuntu and the docker version is 19.03.13. I tried to use 'systemctl start docker' to start but there is something wrong so I tried 'sudo /etc/init.d/docker start'. Didn't work either...
  • Daniel N.
    Daniel N. over 3 years
    I understand. It sounds a bit like WSL... please try "sudo service docker start" and see if it's working. This is in case it's WSL, have you tried to start it as administrator (by right clicking from start menu)?
  • ming
    ming over 3 years
    "sudo service docker start" didn't work so I opened the windows powershell(administrator), run "docker-compose build " and the other error happened'/usr/bin/env: ‘python3\r’: No such file or directory ERROR: Service 'frontend' failed to build : The command '/bin/sh -c ./frontend_assets.py' returned a non-zero code: 127'
  • ming
    ming over 3 years
    It seems like I have to install dos2unix in ubuntu...so I backed to ubuntu, tried to install powershell into ubuntu...after installed powershell and dos2unix, run 'docker version' still cannot connect..error:'Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?'
  • Daniel N.
    Daniel N. over 3 years
    Yes, until docker daemon does not start (the "sudo service docker start" supposed to do that), the socket file doesn't exists and docker and compose cannot connect to the daemon. First thing you should do is find out why docker daemon doesn't start. Please try and follow the informations here (including trying to run dockerd manually and also see the troubleshooting guide on the same page): docs.docker.com/config/daemon
  • JackLeEmmerdeur
    JackLeEmmerdeur over 3 years
    Wonder why isn't this upvoted more? Should be a common error and this was the spot-on answer.
  • zkvarz
    zkvarz almost 3 years
    For me it was related to enabled corporate VPN. I disabled it and then docker started up successfully....
  • n8jadams
    n8jadams almost 3 years
    Saw this error. Panicked. Saw this response. Calm.
  • Zaffer
    Zaffer almost 3 years
    This is what worked for me, I am running ubuntu on WSL2.
  • Elias Prado
    Elias Prado over 2 years
    My issue was that the docker wasn't initiated. Then, I restarted the computer, relaunched docker and everything worked fine.
  • Chau Loi
    Chau Loi over 2 years
    This works for me, I am running ubuntu on Centos
  • Leviathan_the_Great
    Leviathan_the_Great over 2 years
    worked for me! I originally checked "systemctl status docker" to see if docker was running—it was. I move onto this answer and it worked great. thanks!
  • SAIF AHMED ANIK
    SAIF AHMED ANIK over 2 years
    Thank you for the solution. I was trying docker commands without starting docker.
  • Khuram
    Khuram over 2 years
    Actually I already this option set, but I turned it off, reload docker, then turn it on and reload the docker and it did the trick for me.
  • Sharpless512
    Sharpless512 over 2 years
    Make sure it starts with administrator
  • David Maze
    David Maze over 2 years
    Be careful! Anyone who can access the Docker socket can pretty trivially root the host. This setting can make sense on a developer system where you give yourself unrestricted sudo access anyways, but it may not be good security practice in other contexts.
  • gdm
    gdm over 2 years
    this is the right solution!
  • Michał S.
    Michał S. about 2 years
    This solution actually helped me and seems to be more safe that setting 666 permisions on docker.sock
  • John Lexus
    John Lexus about 2 years
    This can be found in Settings -> General -> User Docker Compose V2 -> Apply and Restart
  • Daniel Walker
    Daniel Walker almost 2 years
    Could you explain why this works?