You type docker ps or docker system prune on Ubuntu and get one of two errors:
permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
They look alike but have different causes. The first means the daemon runs but your user may not talk to it. The second means the daemon is not running, or Docker is installed in a way your shell does not reach. Running every command with sudo hides the problem, and files your containers create end up owned by root.
Case 1: permission denied
The Docker daemon listens on /var/run/docker.sock, which belongs to root and the docker group. Your user must be in that group.
groups # is "docker" in the list?
sudo usermod -aG docker $USER
Then sign out and back in (or restart). A new group only applies to new login sessions; newgrp docker applies it to the current terminal only. Check with docker run --rm hello-world.
If groupadd: group 'docker' does not exist, create it first with sudo groupadd docker, then restart Docker.
Be aware that the docker group is as powerful as root: anyone in it can start a container that mounts /. On a shared machine, use rootless Docker instead (dockerd-rootless-setuptool.sh install), which runs the daemon as your user.
Case 2: the daemon is not running
systemctl status docker
sudo systemctl start docker
sudo systemctl enable docker # start it at every boot
If status says the unit failed, journalctl -u docker -n 50 shows why; a full disk is a common cause, since the daemon cannot write to /var/lib/docker. In that case free space elsewhere on the disk, start the daemon, then prune Docker (see below).
Snap Docker (snap list docker) runs as snap.docker.dockerd instead: sudo snap start docker. Its group setup differs: sudo addgroup --system docker, add your user, then sudo snap disable docker && sudo snap enable docker.
Docker Desktop for Linux runs its own VM and uses a different context. docker context ls shows which one is active; docker context use default goes back to the system daemon.
After it works: free the disk space
Once docker answers, docker system df shows images, containers, volumes and build cache, and docker system prune removes what nothing uses. The Docker disk space guide explains each command and what it keeps; for Podman, see Podman using too much disk space on Ubuntu.
How CleanMate does it
CleanMate 2.7.0 on Ubuntu (all Ubuntu features) lists Docker’s dangling images, build cache, stopped containers, unused images and unused volumes in Smart Scan › Developer Junk. When Docker is installed but not reachable, these rows are replaced by one row, Docker: not reachable, that names the reason and shows the fix:
- daemon not running →
sudo systemctl start docker - permission denied →
sudo usermod -aG docker $USER, then sign out and back in
Nothing is deleted from that row, and CleanMate never runs Docker as root to get around the error. After the fix, scan again and the Docker items appear. For the rest of a developer machine, see Free up disk space on Ubuntu as a developer and Virtual machines and dev infrastructure tools.
FAQ
Should I just use sudo with docker? It works, but files created by containers end up owned by root and every script needs sudo. Join the docker group or use rootless Docker.
I added myself to the group and it still says permission denied. Sign out and back in: the current session still has the old groups. id -nG shows the groups it has.
Is adding my user to the docker group safe? It gives root-level access through Docker. That is usual on a personal machine; on a shared one, use rootless Docker.
Docker says no space left on device. Can I delete /var/lib/docker? Only with the daemon stopped, and you lose every image, container and volume. Try docker system prune first.
