43

The Docker service is clearly running:

$ systemctl status docker.service 
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2015-12-28 19:20:50 GMT; 3 days ago
     Docs: https://docs.docker.com
 Main PID: 1015 (docker)
   CGroup: /system.slice/docker.service
           └─1015 /usr/bin/docker daemon -H fd:// --exec-opt native.cgroupdriver=cgroupfs
$ ps wuf -u root | grep $(which docker)
root      1015  0.0  0.3 477048 12432 ?        Ssl   2015   2:26 /usr/bin/docker daemon -H fd:// --exec-opt native.cgroupdriver=cgroupfs

However, Docker itself refuses to talk to it:

$ docker info
Cannot connect to the Docker daemon. Is the docker daemon running on this host?

I am running the default Docker configuration, that is, I haven't changed any /etc files relating to this service.

What could be the problem here?

1
  • i think my problem was nearby the same, but not sure. in my case i got the error unix://home/§USER/.docker/desktop/docker.sock is not reachable. Fixed it by adding env export DOCKER_HOST=unix:///var/run/docker.sock but i think this is not the best way.
    – mtizziani
    Commented Nov 16, 2022 at 21:14

10 Answers 10

56

This question has already been answered, but here's an additional piece of information.

No matter if you're on Arch or another distribution like Fedora or Ubuntu, Docker uses a socket file to communicate. When you run docker commands, it uses this socket to talk to the Docker daemon. Of course, the daemon must be running (and it's often disabled by default), but if your user can't access the socket, it wouldn't be able to communicate with the daemon either.

You would first install Docker from the distribution's repository. Some people download an install script and pipe it to a shell (curl ... | sh), but it's recommended to install it from the repository so it can be updated easily.

Arch:

# pacman -S docker

Fedora:

# dnf install docker

As mentioned above, the daemon may be disabled by default. If you want to use Docker, the daemon must be running.

Enable it (so it will be started on boot):

# systemctl enable docker

Start it now (or reboot):

# systemctl start docker

Now, by default (if the docker group is missing), the Docker socket is owned by root:

# ls -la /var/run/docker.sock
srw-rw---- 1 root root 0 Apr 28 17:22 /var/run/docker.sock

This is why a regular user is not able to talk to the docker daemon. A regular user does not have sufficient permissions to access the socket. It's not able to reach the daemon, so it assumes it's not running and shows this error: Cannot connect to the Docker daemon. Is the docker daemon running on this host?

This is why many people simply start all Docker commands as root, using sudo. But as described in the other answer, Docker has its own mechanism for that, so using sudo is not necessary.

Ideally, a group called docker is created when installing Docker. However, if that group does not exist when the daemon is started, the socket file is owned by root.

In some cases, that group used to have a different name, like dockerroot on Fedora. Check grep docker /etc/group to see if there is such a group on your system. If you're already using that group (your user is in it), you would need to configure Docker to use it:

In /etc/sysconfig/docker, add -G dockerroot (note: it's a workaround, not the best solution):

OPTIONS='--selinux-enabled -G dockerroot'

After restarting the daemon, your user will be able to access the socket:

# systemctl restart docker
# ls -la /var/run/docker.sock
srw-rw---- 1 root dockerroot 0 Apr 28 17:32 /var/run/docker.sock

Otherwise, the official way would be to use the group called docker. If it exists, Docker will automatically use it, i.e., set the socket's group to that group. If it doesn't exist, all you need to do is create it and restart the daemon:

# groupadd docker
# systemctl restart docker

The socket file will be owned by that group:

# srw-rw---- 1 root docker 0 Apr 28 17:42 /var/run/docker.sock

Your user must be in the docker group to be able to access the socket:

# usermod -aG docker (user)

You may have to log out and log back in again (or su - (user)), run id to see if you're in the group.

You can then use Docker without sudo/root:

$ docker version --format '{{.Server.Version}}'
1.9.1

Finally, a word of warning. Only trusted users should be allowed to control your Docker daemon. See https://docs.docker.com/engine/security/security/.
(But of course, the same is true for sudo - only trusted users should be in the wheel group.)

9
  • 1
    This doesn't seem to answer the original question.
    – l0b0
    Commented Apr 28, 2016 at 17:21
  • 5
    @l0b0: Well, I wanted to explain why this is happening, hoping it would be useful to someone. The usermod command that adds the user to the group is hidden in the middle part of the answer. If you don't find this answer useful, let me know so I can delete it.
    – basic6
    Commented Apr 29, 2016 at 11:41
  • 6
    Yes, it does answer and explains the solution for Arch.
    – kodeart
    Commented May 25, 2016 at 7:51
  • I have done all of this, and I am still encountering the issue mentioned by OP. None of the permission related steps have solved the problem.
    – mopsyd
    Commented Jul 20, 2018 at 16:09
  • 2
    this should be the accepted answer
    – Alejandro
    Commented Dec 28, 2019 at 15:18
39

You need to add yourself to the docker group and activate the group (by logging out and in again or running newgrp docker) to run docker commands. The error message is simply misleading.

4
  • 1
    Not really misleading. It couldn't connect to the docker daemon. "is the daemon running?" was just a guess.
    – Bratchley
    Commented Feb 24, 2016 at 16:38
  • 2
    sudo gpasswd -a alex docker Commented Aug 7, 2017 at 8:12
  • 1
    sudo gpasswd -a $USER docker # Works for any username
    – priomsrb
    Commented Jun 28, 2019 at 1:58
  • I did this and it still didn't work. Running sudo systemctl start docker fixed it, the daemon really wasn't running...
    – antonky
    Commented Sep 24, 2019 at 14:28
3

After doing some research into solving this problem on my Linux system I thought I would write this answer. Here is what I did to fix the problem.

On Fedora 22

Installing Docker:

$> curl -fsSL https://get.docker.com/ | sh

After installing Docker:

A user needs to be added to the docker group.

$> sudo usermod -aG docker

The docker daemon needs to be started

$> sudo service docker start

You can set the daemon to start at boot

$> sudo chkconfig docker on

You can verify the docker service is running

$> service docker status

And one last final check

$> docker run hello-world
1
  • +1 for a complete example, although many of these commands are not applicable to my situation (installing using pacman, using systemctl instead of service+chkconfig).
    – l0b0
    Commented Feb 24, 2016 at 17:22
3

sudo usermod -aG docker [username]

then logout then log back in

2

This commands works for me

sudo grep dwalsh /etc/sudoers

alias docker="sudo /usr/bin/docker"

I found the solution in this page if you need more documentation. Why we don't let non-root users run Docker in CentOS, Fedora, or RHEL

2

These are the steps I followed to fix the following

$ docker info
Cannot connect to the Docker daemon at 
tcp://localhost:2375/usr/lib/systemd/system/docker.socket. Is the docker daemon running?
  1. Add your self to the docker group

    usermod -aG docker $USER

  2. Fix permissions on docker socker and command.

    sudo chgrp docker /usr/bin/docker
    sudo chgrp docker /var/run/docker.sock

    $ ll $(which docker) -rwxr-xr-x 1 root docker 18991768 08.07.2017 22:57 /usr/bin/docker*

    $ ll /var/run/docker.sock srw-rw---- 1 root docker 0 23.07.2017 10:21 /var/run/docker.sock

  3. Add variables to config environment for docker command

    export DOCKER_HOST=unix:///var/run/docker.sock

  4. Rest Docker

    sudo systemctl restart docker

1

If you're using Fedora 23 or Redhat variant edit /etc/sysconfig/docker and modify the following

OPTIONS='--selinux-enabled --log-driver=journald -G yourdockergroup'

Restart docker.

Make sure you add this group to the system and add yourself to the group.

1

If you started your docker engine with: sudo service docker start

you can not connect with normal user even if you have added yourself to the 'docker' group.

You can simply stop it with: sudo service docker stop

and start it as a normal user: service docker start

1
  • I did not have to add user to docker group till now. sudo service start worked for me. HOwever, will observe if there is something new.
    – Jitendra
    Commented May 23, 2019 at 17:12
0

I also had the same issue. The problem was in sockets allocated to docker-daemon and docker-client.

  1. First, permission was not set for the docker-client on docker.sock You can set it using sudo usermod -aG docker $USER

  2. Then check your bash file where the docker-client is running. For me it was set to 0.0.0.0:2375, while docker-daemon was running on unix socket. (It was set in the configuration file of dockerd).

  3. Just comment out the offending line and it'll work fine.

  4. But if you want to make it work on TCP port instead of unix socket, then change the configuration file of dockerd, set it to 0.0.0.0.2375, and keep the line in bash as it is if present or set it to 0.0.0.0:2375.

0

I had the similar issue, it turns out I got it because I was already running a shell in the docker container! I opened another terminal and type docker ps and it worked fine!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .