Docker command can't connect to Docker daemon

252,735

Solution 1

You need to add your current user to the docker group as follows:

sudo usermod -aG docker $(whoami)

then logout & login again into the system or restart the system. test by docker version

for further info how to install docker-engine follow docker documentation

Solution 2

Add the user to the docker group

  • Add the docker group if it doesn't already exist:

    sudo groupadd docker

  • Add the connected user "${USER}" to the docker group:

    sudo gpasswd -a ${USER} docker

  • Restart the Docker daemon:

    sudo service docker restart

  • Either do a newgrp docker or log out/in to activate the changes to groups.

Solution 3

Usually, the following command does the trick:

sudo service docker restart

This, instead of docker start for the cases where Docker seems to already be running.

If that works then, as suggested and in another answer and on this GitHub issue, if you haven't added yourself in the docker group do it by running:

sudo usermod -aG docker <your-username> 

And you're most likely good to go.


As for anybody else bumping into this, in some OS's docker doesn't start right after you install it and, as a result, the same can't connect to daemon message appears. In this case you can first verify that Docker is indeed not running by checking the status of your docker service by executing:

sudo service docker status

If the output looks something like: docker stop/waiting instead of docker start/running, process 15378 then it obviously means Docker is not active. In this case make sure you start it with:

sudo service docker start

And, as before, you'll most likely be good to go.

Solution 4

note to self: I get the error from the question's title when I forget to run docker command with sudo:

sudo docker run ...

[Ubuntu 15.10]

Solution 5

Had the same issue and what worked for me was:
Checking the ownership of /var/run/docker.sock

ls -l /var/run/docker.sock

If you're not the owner then change ownership with the command

sudo chown *your-username* /var/run/docker.sock

Then you can go ahead and try executing the docker commands hassle-free :D

Share:
252,735

Related videos on Youtube

kramer65
Author by

kramer65

Updated on July 08, 2022

Comments

  • kramer65
    kramer65 almost 2 years

    I want to make a move to Docker, so I've just started to mess around with it. I've installed Docker on a VirtualBox Ubuntu 15.10 (Wily Werewolf) installation and as suggested here I then tried running a basic nginx Docker image:

    $ docker run --name mynginx1 -P -d nginx
    Cannot connect to the Docker daemon. Is the docker daemon running on this host?
    

    So I checked out whether Docker was running:

    $ sudo service docker status
    ● docker.service - Docker Application Container Engine
       Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
       Active: active (running) since vr 2015-11-06 08:41:48 CET; 15min ago
         Docs: https://docs.docker.com
     Main PID: 7542 (docker)
       CGroup: /system.slice/docker.service
               └─7542 /usr/bin/docker daemon -H fd://
    
    nov 06 08:41:47 kramer65-VirtualBox systemd[1]: Starting Docker Application Container Engine...
    nov 06 08:41:47 kramer65-VirtualBox docker[7542]: time="2015-11-06T08:41:47.900410966+01:00" level=info msg="API ...ock"
    nov 06 08:41:48 kramer65-VirtualBox docker[7542]: time="2015-11-06T08:41:48.033514149+01:00" level=info msg="Fire...lse"
    nov 06 08:41:48 kramer65-VirtualBox docker[7542]: time="2015-11-06T08:41:48.141594321+01:00" level=info msg="Defa...ess"
    nov 06 08:41:48 kramer65-VirtualBox docker[7542]: time="2015-11-06T08:41:48.416294436+01:00" level=warning msg="Y...it."
    nov 06 08:41:48 kramer65-VirtualBox docker[7542]: time="2015-11-06T08:41:48.565507576+01:00" level=info msg="Load...rt."
    nov 06 08:41:48 kramer65-VirtualBox docker[7542]: time="2015-11-06T08:41:48.567907022+01:00" level=info msg="Load...ne."
    nov 06 08:41:48 kramer65-VirtualBox docker[7542]: time="2015-11-06T08:41:48.567945214+01:00" level=info msg="Daem...ion"
    nov 06 08:41:48 kramer65-VirtualBox docker[7542]: time="2015-11-06T08:41:48.567969891+01:00" level=info msg="Dock....9.0
    nov 06 08:41:48 kramer65-VirtualBox systemd[1]: Started Docker Application Container Engine.
    Hint: Some lines were ellipsized, use -l to show in full.
    

    This suggests that the Docker daemon is actually already running, but to be sure I just started the Docker daemon manually:

    $ sudo docker daemon
    INFO[0000] API listen on /var/run/docker.sock           
    INFO[0000] [graphdriver] using prior storage driver "aufs" 
    INFO[0000] Firewalld running: false                     
    INFO[0000] Default bridge (docker0) is assigned with an IP address XXX.XX.X.X/XX. Daemon option --bip can be used to set a preferred IP address 
    WARN[0000] Your kernel does not support swap memory limit. 
    INFO[0000] Loading containers: start.                   
    
    INFO[0000] Loading containers: done.                    
    INFO[0000] Daemon has completed initialization          
    INFO[0000] Docker daemon                                 commit=76d6bc9 execdriver=native-0.2 graphdriver=aufs version=1.9.0
    

    I then tried running the image again, but with the same result:

    $ docker run --name mynginx1 -P -d nginx
    Cannot connect to the Docker daemon. Is the docker daemon running on this host?
    

    I tried sudo'ing the command, but to no avail. What am I doing wrong here?

    • Daniel Loureiro
      Daniel Loureiro over 8 years
      same problem. I'm on Ubuntu 15.10 too :(
    • Lucas Tettamanti
      Lucas Tettamanti over 8 years
      same problem, ubuntu 15.10
    • Nasruddin
      Nasruddin over 8 years
      Just restart your docker service that's all. Thanks @jim <pre>sudo service docker restart</pre>
    • Rafael_Espericueta
      Rafael_Espericueta almost 8 years
      Nasruddin, that sure did NOT work for me... :-(
    • romaroma
      romaroma over 7 years
      I had to restart the machine to make it work.
    • muon
      muon over 7 years
      I have to use sudo even after adding user
    • Eugen Konkov
      Eugen Konkov over 7 years
    • Elaine
      Elaine almost 7 years
      I got this error with Azure Container Service master machine, and failed to fix this by any solution provided here, a one-click restart resolved this :(
  • Alar
    Alar over 8 years
    that "wget" thing just put the docker repository in your apt source....i.e. the same thing you are asked to manually do in the "correct and official way"....
  • Daniel Loureiro
    Daniel Loureiro over 8 years
    I'm afraid you are wrong, Alar. The "wget" method will add docker repository in your apt source, which is good, but it will also do many other bad things like set a DOCKER_HOST env var, which will prevent docker to run out-of-the-box. Is not because both methods add docker to the repo that they are both the same.
  • benka
    benka over 8 years
    silly but it worked for me too @RobinLoxley, this should be an actual answer not just a comment, let me know if you post it, i'll give you an upvote ;)
  • johntellsall
    johntellsall about 8 years
    Step 2 can be simplified as: sudo usermod -aG docker $USER
  • Michele
    Michele about 8 years
    I would underline "logout & login again into the system or restart the system"
  • RhinoDevel
    RhinoDevel about 8 years
    "I tried sudo'ing the command, but to no avail. Does anybody know what I'm doing wrong here? All tips are welcome!"
  • brandizzi
    brandizzi about 8 years
    @Junaid answer below shows how to avoid the logout/login step, in case you are, like me, so lazy you don't want to restart your apps ;)
  • basic6
    basic6 about 8 years
    On Fedora 23, this group may be called dockerroot. Also, you probably want to install it from the repository (instead of curl ... | sh) so it can be updated later: dnf install docker
  • bergey
    bergey about 8 years
    It seems that newgrp docker is local to the shell, not the login session. This is not at all clear from the man page.
  • TonyTony
    TonyTony almost 8 years
    many do not have a group "docker", so we have to add it first groupadd docker and then add it to your username sudo usermod -aG docker your_username
  • aalaap
    aalaap almost 8 years
    This is exactly what's mentioned in the Docker tutorial, but logging out and back in again didn't work for me. I had to restart the system to get it working.
  • Apokai
    Apokai almost 8 years
    Worked for me. Thanks!
  • ghanbari
    ghanbari almost 8 years
    I see docker version information, but in the end of output, i see Cannot connect to the Docker daemon. Is the docker daemon running on this host?
  • Zhang LongQI
    Zhang LongQI almost 8 years
    This is required if you only logout and login.
  • buhtz
    buhtz over 7 years
    Please format this as code! And explain this command. And use the long (starting with --) versions of options.
  • yarin
    yarin over 7 years
    Works for vagrant machine as well
  • gerrit_hoekstra
    gerrit_hoekstra about 7 years
    After exhausting all the other possibilities, this is what got it to work on Sabayon/Gentoo Linux for me.
  • Meghna Natraj
    Meghna Natraj over 6 years
    The only answer that worked! On a private cloud Linux machine, and it still worked!
  • chris
    chris over 6 years
    On CentOS 7 this group is called dockerroot, and I also had to add "group": "dockerroot" to /etc/docker/daemon.json to ensure that my user could access the docker.sock file.
  • Abhinav Manchanda
    Abhinav Manchanda over 6 years
    Just to add here, if you're using AWS, you'll need to reboot the EC2 instance.
  • Kshitij Saraogi
    Kshitij Saraogi almost 5 years
    Worked on Ubuntu 18.04! Thanks
  • Lucas Bustamante
    Lucas Bustamante almost 5 years
    That file is owned by root but in docker group. The correct approach is to assign your current user to the docker group, according to Docker's documentation: sudo groupadd docker && sudo usermod -aG docker $USER
  • therobyouknow
    therobyouknow over 4 years
    @Federico - "no need to log out, use: exec su -l $USER " - thank you +1
  • ashishSober
    ashishSober almost 3 years
    /var/run# service docker status [FAIL] Docker is not running ... failed!