Docker Socket Exists but Connection Is Refused: Debugging a snap + apt Double Installation
Published:
One day, running docker ps returned the classic error:
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
Classic, but strange. pgrep dockerd showed that the daemon was running, /var/run/docker.sock existed, and ss -xlnp clearly showed the socket in the LISTEN state. The user was also in the docker group. Everything looked normal, but the connection was still refused with ECONNREFUSED.
Step 1: Check “Which Docker” Is Running
pgrep -a dockerd gave the first clue:
dockerd --group docker --exec-root=/run/snap.docker
--data-root=/var/snap/docker/common/var-lib-docker
--config-file=/var/snap/docker/3505/config/daemon.json
The paths were full of snap: the daemon came from a snap installation.
Then I checked the CLI:
$ which docker
/usr/bin/docker
$ dpkg -S /usr/bin/docker
docker-ce-cli: /usr/bin/docker
The CLI was docker-ce-cli installed through apt, version 29.4.0. The snap daemon was 29.3.1.
Having two installations is not fatal by itself. The Docker CLI and daemon communicate through a Unix socket, and API version compatibility is usually enough. But this environment really did have two independent Docker installations from two package managers.
Step 2: Use strace to Inspect the System Call
curl --unix-socket also failed, which meant this was not a Docker CLI issue. I used strace to see what was actually happening:
connect(5, {sa_family=AF_UNIX, sun_path="/run/docker.sock"}, 19)
= -1 ECONNREFUSED (Connection refused)
ECONNREFUSED. The socket file existed, and ss said it was listening, but the kernel refused the connection. This usually means the listening endpoint is not truly accepting connections.
Step 3: Find the Root Cause in journalctl
journalctl -u snap.docker.dockerd --no-pager | tail -30
The logs repeatedly showed a few error patterns.
Leftover container conflict:
failed to start container: failed to create task for container:
OCI runtime create failed: runc create failed:
container with given ID already exists
Timeout while cleaning up a dead shim:
failed to delete shim: close wait error: context deadline exceeded
Then cleanup could not find the container:
cleanup: failed to delete container from containerd:
NotFound: container "xxx" in namespace "moby": not found
The full failure chain was:
- When Docker daemon starts, it tries to recover containers that were previously marked as running.
- The containerd shim processes for those containers no longer exist, but the runc state directories are still there.
- The daemon tries to create a task, and runc reports “already exists”.
- The daemon tries to clean up, and containerd reports “not found”.
- Cleanup times out, and the daemon gets blocked in its startup loop.
- The socket file is created, but the API never becomes truly ready, so every connection is refused.
In essence, this was inconsistent Docker state storage, more precisely containerd plus runc state inconsistency. The containers were marked as running in Docker’s database, but at the containerd/runc layer they were half-dead zombie state.
Fix
Clean the leftover state and start fresh:
sudo snap stop docker.dockerd
# Clean containerd task state
sudo rm -rf /run/snap.docker/containerd/daemon/io.containerd.runtime.v2.task/moby
# Clean Docker's container records
sudo rm -rf /var/snap/docker/common/var-lib-docker/containers/*
sudo snap start docker.dockerd
After that, docker ps returned normally.
Checklist
When Docker’s socket exists but cannot be connected to, debug in this order:
pgrep dockerd: is the daemon running?strace -e connect curl --unix-socket /run/docker.sock http://localhost/version: is the failureENOENTorECONNREFUSED?journalctl -u snap.docker.dockerdfor snap, orjournalctl -u dockerfor apt: what is the daemon doing?- If logs contain
runc create failed: container with given ID already exists, it is a leftover-state problem. Clean the task directory under/run/snap.docker/containerd/.
This sequence covers the key information sources on the path from “everything looks normal” to the actual root cause.

Leave a Comment