Docker: "permission denied while trying to connect to the Docker daemon socket"
Fix Docker "permission denied" on /var/run/docker.sock: add your user to the docker group, make it take effect, and know why chmod 666 is a bad idea.
by OhSudo ··
Not sure this is your cause?Find it step by step: Container won't start →
Part of a learning path:Docker: from first container to production
You run a Docker command without sudo and get:
$ docker pspermission denied while trying to connect to the docker API at unix:///var/run/docker.sockDocker versions before 29 say more, with the request they were making:
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 deniedThe meaning is the same. The docker command is only a client: it talks to the Docker daemon through the socket file /var/run/docker.sock. That file belongs to root and to the group docker, and your user is neither. With sudo it works, because root may open it.
- Your usernot root, not in the docker grouppermission denied(fails)
- /var/run/docker.sockowned by root, group docker(breaks here)leads to
- dockerdrunning, never reached
TL;DR
sudo usermod -aG docker $USER # add yourself to the docker group# log out and back in (or reconnect over SSH), then:docker ps # works without sudo1. Check the socket and your groups
srw-rw---- 1 root docker 0 Nov 1 09:02 /var/run/docker.sockuid=1000(guest) gid=1000(guest) groups=1000(guest),27(sudo)The socket is readable and writable by root and the docker group (rw-rw----), and docker is missing from your groups. That’s the whole problem.
2. Add your user to the docker group
sudo usermod -aG docker $USER-aG means “append to this Group”. Without -a, usermod -G replaces all your other groups — and you could lose sudo.
If you get usermod: group 'docker' does not exist, the package didn’t create it (it happens with some installs). Create it, and restart Docker so the socket gets the group:
sudo groupadd dockersudo usermod -aG docker $USERsudo systemctl restart docker3. Make it take effect
This is where most people get stuck: you’re in the group, and it still says permission denied. Linux reads your groups when you log in. A shell that was already open keeps the old list — id still doesn’t show docker.
- Log out and back in. Over SSH, disconnect and connect again.
- Or, for the current shell only:
newgrp docker. - VS Code Remote / tmux / screen keep a session running in the background, so a reconnect isn’t enough: restart the remote server, or the tmux / screen session.
- Services that run Docker commands (Jenkins, a CI runner) also read their groups only when they start:
sudo usermod -aG docker jenkins, thensudo systemctl restart jenkins.
Check with id — docker should be in the list — then run docker ps again.
Warning
The docker group is as powerful as root. Anyone in it can run docker run -v /:/host … and read or change every file on the machine. Add only people who could have sudo anyway. For a hardened setup, look at rootless Docker, which runs the daemon as your user.
Don’t “fix” it with chmod
Many answers online suggest:
sudo chmod 666 /var/run/docker.sock # don'tIt “works” because now every user and every process on the machine can control Docker — which, as above, means they can become root. It also doesn’t last: Docker re-creates the socket with the normal permissions when it restarts. Use the group.
The daemon isn’t running
A different message means a different problem:
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?Here the socket isn’t there at all, because Docker isn’t running. Start it, and make it start at boot:
sudo systemctl enable --now dockersystemctl status docker --no-pagerIf it fails to start, the reason is in sudo journalctl -xeu docker — see Job for … failed because the control process exited with error code.
Shell, we have a problem!
Similar, but different: with Docker Desktop on Linux, the CLI talks to Desktop’s own virtual machine through another socket. docker context ls shows which one is active (desktop-linux or default), and docker context use default switches back to the system Docker Engine. In rootless Docker, the socket is in your home runtime folder, so DOCKER_HOST must point to unix://$XDG_RUNTIME_DIR/docker.sock. If the daemon isn’t running at all, see Cannot connect to the Docker daemon. For the commands around this, see the Docker CLI Cheat Sheet.
$ ./practice.sh
Practice this in the terminal
Open the Terminal Playground with this article’s scenario loaded. It is safe and runs in your browser.
Found this useful? Share it.
Found a mistake? Let us know at contact@ohsudo.com.