Share
Cannot Connect to a Docker Daemon
Cannot Connect to a Docker Daemon

Cannot Connect to a Docker Daemon: Let’s first understand the Docker daemon (dockerd). It is a program that manages all the Docker objects, including images, containers, volumes and many more.

Cannot Connect to a Docker Daemon

Due to Inactive Docker Service

The most common reason for this error is when we try to access the Docker service, but it is not started:

$ docker ps

Cannot connect to the Docker daemon at unix:///var/run/docker.sock.

  Is the docker daemon running?

First, we will check the status of the Docker service and whether it is running or not:

$ systemctl status docker

 docker.service – Docker Application Container Engine

   Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)

   Active: inactive (dead)

   Docs: https://docs.docker.com

Here the output clearly shows that the Docker service is inactive.

We’ll now start the Docker service. This will solve the issue in most cases

Read More ….. Solving: Errordomain=nscocoaerrordomain&errormessage=could not find the specified shortcut.&errorcode=4

Start Docker Using Service

Whenever we use a package manager and install docker it creates a docker service. This makes it even easier to manage docker.

Let’s now start the Docker using the systemctl service command:

$ systemctl start docker

We can check the status of Docker using the following command:

$ systemctl status docker

 docker.service – Docker Application Container Engine

   Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)

   Active: active (running) since Thu 2022-02-17 19:14:51 UTC; 1min 38s ago

     Docs: https://docs.docker.com

 Main PID: 1831 (dockerd)

    Tasks: 8

   Memory: 126.5M

   CGroup: /system.slice/docker.service

           └─1831 /usr/bin/dockerd -H fd:// –containerd=/run/containerd/containerd.sock

This command shows the current status Docker service as active (running).

Start the Docker Daemon Manually

There are many options as well which allow us to start docker without any service.

All we need to do is run the dockerd command in the background:

$ sudo dockerd

INFO[2022-02-18T05:19:50.048886666Z] Starting up                                 

INFO[2022-02-18T05:19:50.050883459Z] libcontainerd: started new containerd process  pid=2331

INFO[2022-02-18T05:19:50.050943756Z] parsed scheme: “unix”                         module=grpc

We need to make sure that we run the dockerd with sudo privileges.

Due to Insufficient Privileges

When we install docker using manage packages it by default creates a docker users and groups to have access to docker we need to add the current user to the docker repository.

If we try to access the Docker service from a user out of the docker group, we get this error:

$ docker ps

Got permission denied while trying to connect to the Docker daemon socket

  at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.40/containers/json:

  dial unix /var/run/docker.sock: connect: permission denied

To solve this issue we can do two things either we can add the user to docker group or we can update the privileges of docker socket file.

Let’s now dive deep into both solutions, along with examples.

Updating User Privileges

User privileges are an important concept in Linux. They determine access to resources for different users.

After installing Docker on Linux a new docker group is created and all the packages related to the docker service are linked to this docker group.

If we do not find the default docker group on our machine, we can create it manually:

$ sudo groupadd docker

Copy

The above command will create a docker group.

Now we’ll add the current user inside the docker group:

$ sudo usermod -aG docker docker-test

Finally, we’ll restart the Docker service to bring the changes into effect:

$ sudo service docker restart

Updating Docker Socket File Privileges

We can also fix the problem by changing the owner of the /var/run/docker.sock file:

$ sudo chown docker-test /var/run/docker.sock

Copy

Notice that we run this command with sudo privileges. Otherwise, the permission for the file won’t update.

Conclusion

In this article, we have learned about Docker domain connectivity issues that we frequently face.

This issue mostly occurs when docker service is not started correctly or when we don’t have proper user to access docker service?

We have shown you different ways to solve this issue by changing the file permissions by adding users to the Doctors group. Cannot Connect to a Docker Daemon. Cannot Connect to a Docker Daemon

Similar Posts