33

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?

42

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.)

| improve this answer | |
  • 1
    This doesn't seem to answer the original question. – l0b0 Apr 28 '16 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 Apr 29 '16 at 11:41
  • 5
    Yes, it does answer and explains the solution for Arch. – kodeart May 25 '16 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 Jul 20 '18 at 16:09
  • 1
    this should be the accepted answer – Alejandro Dec 28 '19 at 15:18
37

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.

| improve this answer | |
  • 1
    Not really misleading. It couldn't connect to the docker daemon. "is the daemon running?" was just a guess. – Bratchley Feb 24 '16 at 16:38
  • 2
    sudo gpasswd -a alex docker – Alex Punnen Aug 7 '17 at 8:12
  • 1
    sudo gpasswd -a $USER docker # Works for any username – priomsrb Jun 28 '19 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... – nakamin Sep 24 '19 at 14:28
3

sudo usermod -aG docker [username]

then logout then log back in

| improve this answer | |
1

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
| improve this answer | |
  • +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 Feb 24 '16 at 17:22
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.

| improve this answer | |
1

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

| improve this answer | |
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

| improve this answer | |
  • 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. – h3xh4wk May 23 '19 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.

| improve this answer | |
0

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

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.