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

Ticker

6/recent/ticker-posts

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


In this tutorial, we will fix one of the real time issue of the “cannot connect to the Docker daemon at unix:///var/run/docker.sock ” error and the ways to solve it.


What is /var/run/docker.sock ?

It is a Unix, when you run any docker command, the docker binary will try to connect to /var/run/docker.sock.

As indicated by its .sock extension, this file is a Unix Domain Socket – basically, a way so multiple processes can communicate on the local computer 

also called an IPC mechanism – IPC = “Inter-Process Communication”, which is used for exchanging the data between the processes.

Any user who belongs to the Docker group should have access to /var/run/docker.sock for sharing the data between the processes.


There are several ways to fix the error. If one solution doesn’t work for you, move on to the next method until you resolve the issue.


First Method : Check the Docker Daemon Running

[root@stapp01 ~]# docker ps

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

[root@stapp01 ~]#

[root@stapp01 ~]# systemctl status docker

● docker.service - Docker Application Container Engine

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

   Active: inactive (dead) since Mon 2022-03-14 15:27:30 UTC; 8min ago

     Docs: https://docs.docker.com

 Main PID: 4244 (code=exited, status=0/SUCCESS)

 

Mar 14 15:27:30 stapp01.stratos.xfusioncorp.com systemd[1]: docker.service: got STOPPING=1

Mar 14 15:27:30 stapp01.stratos.xfusioncorp.com systemd[1]: Child 4244 belongs to docker.service

Mar 14 15:27:30 stapp01.stratos.xfusioncorp.com systemd[1]: docker.service: main process exited, code=exited, status=0/SUCCESS

Mar 14 15:27:30 stapp01.stratos.xfusioncorp.com systemd[1]: docker.service changed stop-sigterm -> dead

Mar 14 15:27:30 stapp01.stratos.xfusioncorp.com systemd[1]: Job docker.service/stop finished, result=done

Mar 14 15:27:30 stapp01.stratos.xfusioncorp.com systemd[1]: Stopped Docker Application Container Engine.

Mar 14 15:27:30 stapp01.stratos.xfusioncorp.com systemd[1]: docker.service: cgroup is empty

Mar 14 15:28:37 stapp01.stratos.xfusioncorp.com systemd[1]: Trying to enqueue job docker.service/start/replace

Mar 14 15:31:50 stapp01.stratos.xfusioncorp.com systemd[1]: Trying to enqueue job docker.service/start/replace

Mar 14 15:33:11 stapp01.stratos.xfusioncorp.com systemd[1]: Trying to enqueue job docker.service/start/replace

[root@stapp01 ~]#

If the Docker Daemon isn’t running, start it with the following command 

[root@stapp01 ~]# systemctl start docker

Failed to start docker.service: Unit is not loaded properly: Invalid argument.

See system logs and 'systemctl status docker.service' for details.

[root@stapp01 ~]#

If the Docker Daemon failed to start with above error the move to check second step

Second Method :  Make sure docker socket file exist with correct parameters 

[root@stapp01 ~]# rpm -ql docker-ce-20.10.13-3.el7.x86_64

/usr/bin/docker-init

/usr/bin/docker-proxy

/usr/bin/dockerd

/usr/lib/systemd/system/docker.service

/usr/lib/systemd/system/docker.socket

[root@stapp01 ~]#

[root@stapp01 ~]# vi /usr/lib/systemd/system/docker.socket

[root@stapp01 ~]# cat  /usr/lib/systemd/system/docker.socket

[Unit]

Description=Docker Socket for the API

PartOf=docker.service

 [Socket]

ListenStream=/var/run/docker.sock

SocketMode=0660

SocketUser=root

SocketGroup=docker

 [Install]

WantedBy=sockets.target

[root@stapp01 ~]#

Third  Method : Assign Ownership to the Docker Unix Socket

The “cannot connect to the Docker daemon” error also happens if the Unix socket file for Docker doesn’t have the correct ownership assigned.

1. Check ownership for the Docker Unix socket

2. Add your user to the Docker group , if you don't have root access

[root@stapp01 ~]# ll /var/run/docker.sock

srw-rw---- 1 root docker 0 Mar 14 15:27 /var/run/docker.sock

[root@stapp01 ~]#
[root@stapp01 ~]# usermod -a -G docker (user)

[root@stapp01 ~]#

Now you can start Docker Daemon  with the following command 

[root@stapp01 ~]# systemctl start docker

Failed to start docker.service: Unit is not loaded properly: Invalid argument.

See system logs and 'systemctl status docker.service' for details.

[root@stapp01 ~]#

 [root@stapp01 ~]# systemctl start docker.socket

Failed to start docker.socket: Unit is not loaded properly: Invalid argument.

See system logs and 'systemctl status docker.socket' for details.

[root@stapp01 ~]#

 If you face above  error while start docker daemon, then run below command to resolve issue 

[root@stapp01 ~]# systemctl daemon-reload

[root@stapp01 ~]#

[root@stapp01 ~]# systemctl start  docker

[root@stapp01 ~]# 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 Mon 2022-03-14 15:43:06 UTC; 4s ago

     Docs: https://docs.docker.com

 Main PID: 5295 (dockerd)

    Tasks: 21

   Memory: 48.9M

   CGroup: /docker/4bc90ad3b709d6b9989067dec258feee0f1be7397152565f4786ca92cad5dd00/system.slice/docker.service

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

 

Mar 14 15:43:05 stapp01.stratos.xfusioncorp.com dockerd[5295]: time="2022-03-14T15:43:05.905238131Z" level=info msg="Loading containers: done."

Mar 14 15:43:06 stapp01.stratos.xfusioncorp.com dockerd[5295]: time="2022-03-14T15:43:06.049608776Z" level=info msg="Docker daemon" com...10.13

Mar 14 15:43:06 stapp01.stratos.xfusioncorp.com dockerd[5295]: time="2022-03-14T15:43:06.049718895Z" level=info msg="Daemon has complet...tion"

Mar 14 15:43:06 stapp01.stratos.xfusioncorp.com systemd[1]: Got notification message for unit docker.service

Mar 14 15:43:06 stapp01.stratos.xfusioncorp.com systemd[1]: docker.service: Got notification message from PID 5295 (READY=1)

Mar 14 15:43:06 stapp01.stratos.xfusioncorp.com systemd[1]: docker.service: got READY=1

Mar 14 15:43:06 stapp01.stratos.xfusioncorp.com systemd[1]: docker.service changed start -> running

Mar 14 15:43:06 stapp01.stratos.xfusioncorp.com systemd[1]: Job docker.service/start finished, result=done

Mar 14 15:43:06 stapp01.stratos.xfusioncorp.com systemd[1]: Started Docker Application Container Engine.

Mar 14 15:43:06 stapp01.stratos.xfusioncorp.com dockerd[5295]: time="2022-03-14T15:43:06.339952514Z" level=info msg="API listen on /var...sock"

Hint: Some lines were ellipsized, use -l to show in full.

[root@stapp01 ~]#


Validate by running

[root@stapp01 ~]# docker ps

CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

[root@stapp01 ~]#


Happy Learning!!!!

Apart from this if you need more clarity,  I have made a  tutorial video on this, please go through and share your comments. Like and share the knowledge




Post a Comment

1 Comments

  1. After executing all the above commands still facing the same issue
    root@treps-tconlinux-01:/usr/lib/systemd/system# cd
    root@treps-tconlinux-01:~# docker ps
    Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
    root@treps-tconlinux-01:~#

    ReplyDelete

Latest Posts