Home > Software engineering >  docker can't connect to docker daemon but socket is created
docker can't connect to docker daemon but socket is created

Time:10-28

My problem is that I can use docker with for example Portainer but when I run docker on the machine on sudo docker can't connect to the daemon and tells me about it:

All commands are done with root.

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

docker service:

systemctl status docker.service
● docker.service - Docker Application Container Engine
     Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
    Drop-In: /etc/systemd/system/docker.service.d
             └─options.conf
     Active: active (running) since Fri 2021-10-22 19:02:54 UTC; 4 days ago
TriggeredBy: ● docker.socket
       Docs: https://docs.docker.com
   Main PID: 182765 (dockerd)
      Tasks: 175
     Memory: 93.1M
     CGroup: /system.slice/docker.service
             ├─182765 /usr/bin/dockerd -H unix:// --containerd=/run/containerd/containerd.sock
             ├─182942 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 49153 -container-ip 172.17.0.2 -container-port 27017
             ├─182949 /usr/bin/docker-proxy -proto tcp -host-ip :: -host-port 49153 -container-ip 172.17.0.2 -container-port 27017
             ├─182962 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 8081 -container-ip 172.18.0.2 -container-port 8080
             ├─182970 /usr/bin/docker-proxy -proto tcp -host-ip :: -host-port 8081 -container-ip 172.18.0.2 -container-port 8080
             ├─182984 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 6881 -container-ip 172.18.0.2 -container-port 6881
             ├─182990 /usr/bin/docker-proxy -proto tcp -host-ip :: -host-port 6881 -container-ip 172.18.0.2 -container-port 6881
             ├─183004 /usr/bin/docker-proxy -proto udp -host-ip 0.0.0.0 -host-port 6881 -container-ip 172.18.0.2 -container-port 6881
             ├─183010 /usr/bin/docker-proxy -proto udp -host-ip :: -host-port 6881 -container-ip 172.18.0.2 -container-port 6881
             ├─183034 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 49154 -container-ip 172.17.0.3 -container-port 3306
             ├─183041 /usr/bin/docker-proxy -proto tcp -host-ip :: -host-port 49154 -container-ip 172.17.0.3 -container-port 3306
             ├─183148 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 49155 -container-ip 172.17.0.4 -container-port 6379
             ├─183154 /usr/bin/docker-proxy -proto tcp -host-ip :: -host-port 49155 -container-ip 172.17.0.4 -container-port 6379
             ├─183332 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 9443 -container-ip 172.17.0.5 -container-port 9443
             ├─183339 /usr/bin/docker-proxy -proto tcp -host-ip :: -host-port 9443 -container-ip 172.17.0.5 -container-port 9443
             ├─183353 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 9000 -container-ip 172.17.0.5 -container-port 9000
             ├─183360 /usr/bin/docker-proxy -proto tcp -host-ip :: -host-port 9000 -container-ip 172.17.0.5 -container-port 9000
             ├─183372 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 8000 -container-ip 172.17.0.5 -container-port 8000
             ├─183378 /usr/bin/docker-proxy -proto tcp -host-ip :: -host-port 8000 -container-ip 172.17.0.5 -container-port 8000
             ├─186468 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 8080 -container-ip 172.17.0.6 -container-port 80
             └─186474 /usr/bin/docker-proxy -proto tcp -host-ip :: -host-port 8080 -container-ip 172.17.0.6 -container-port 80

Oct 27 09:31:30 falcon dockerd[182765]: time="2021-10-27T09:31:30.218332206Z" level=debug msg="Calling GET /containers/69047b41bedea4794803730ff0fa13a65e546519f9>
Oct 27 09:31:30 falcon dockerd[182765]: time="2021-10-27T09:31:30.219026938Z" level=debug msg="Calling GET /images/json?all=0"
Oct 27 09:32:58 falcon dockerd[182765]: time="2021-10-27T09:32:58.654321683Z" level=debug msg="Calling HEAD /_ping"
Oct 27 09:32:58 falcon dockerd[182765]: time="2021-10-27T09:32:58.656492046Z" level=debug msg="Calling GET /v1.37/info"
Oct 27 09:32:58 falcon dockerd[182765]: time="2021-10-27T09:32:58.673377621Z" level=debug msg="Calling GET /v1.37/containers/json?all=1&limit=0"
Oct 27 09:32:58 falcon dockerd[182765]: time="2021-10-27T09:32:58.680766521Z" level=debug msg="Calling GET /v1.37/images/json"
Oct 27 09:32:58 falcon dockerd[182765]: time="2021-10-27T09:32:58.701618241Z" level=debug msg="Calling GET /v1.37/volumes"

I can use portainer as usual

docker version:

docker -v
Docker version 20.10.7, build 20.10.7-0ubuntu1~20.04.2

More information:

ls -l /var/run
lrwxrwxrwx 1 root root 4 Jul 31  2020 /var/run -> /run

docker.sock is created:

drwxr-xr-x  2 root root   40 Oct 25 20:31 docker.sock
rcat /usr/lib/systemd/system/docker.service
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service containerd.service
Wants=network-online.target
Requires=docker.socket
Wants=containerd.service

[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always

# Note that StartLimit* options were moved from "Service" to "Unit" in systemd 229.
# Both the old, and new location are accepted by systemd 229 and up, so using the old location
# to make them work for either version of systemd.
StartLimitBurst=3

# Note that StartLimitInterval was renamed to StartLimitIntervalSec in systemd 230.
# Both the old, and new name are accepted by systemd 230 and up, so using the old name to make
# this option work for either version of systemd.
StartLimitInterval=60s

# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity

# Comment TasksMax if your systemd version does not support it.
# Only systemd 226 and above support this option.
TasksMax=infinity

# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes

# kill only the docker process, not all processes in the cgroup
KillMode=process
OOMScoreAdjust=-500

[Install]
WantedBy=multi-user.target

Any one got a clue why docker is unable to connect to the daemon?

CodePudding user response:

It looks like you have a container configured to bind mount /var/lib/docker.sock and the daemon restarted that container before creating the socket. There's been some tweeks to packaging in recent releases to reduce this chance. Otherwise you may want to mount the entire directory instead of a single file.

To fix, try stopping docker, deleting the empty directory, and restarting docker to see if the socket gets created first (it's a race condition).

  • Related