Fix "permission denied while trying to connect to the Docker daemon socket"
permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get "http://%2Fvar%2Frun%2Fdocker.sock/v1.24/containers/json": dial unix /var/run/docker.sock: connect: permission denied
- Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post "http://%2Fvar%2Frun%2Fdocker.sock/v1.24/images/create": dial unix /var/run/docker.sock: connect: permission denied
- permission denied while trying to connect to the docker API at unix:///var/run/docker.sock
- ERROR: permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Head "http://%2Fvar%2Frun%2Fdocker.sock/_ping": dial unix /var/run/docker.sock: connect: permission denied
The docker client is not allowed to open the /var/run/docker.sock socket, which belongs to root and the docker group. The usual fix on Linux: add your user to the docker group with sudo usermod -aG docker $USER, then log out and back in.
What this error means
The docker command does not run anything itself: it sends HTTP requests to the dockerd daemon through a Unix socket, usually /var/run/docker.sock. That file has permissions like srw-rw---- root docker: only root and members of the docker group can connect. If your user is not in that group, or your session has not picked up the change yet, the kernel refuses the connection (connect: permission denied). The daemon is running: access to the socket is what is blocked, unlike "Cannot connect to the Docker daemon", which means the daemon is stopped.
Common causes
- User not in the
dockergroup: the typical case right after installing Docker Engine on Ubuntu, Debian or Fedora. - Session not refreshed: you ran
usermod -aG docker, but a session’s groups are only read at login. The open terminal does not see the new group yet. - CI service user: Jenkins, GitLab Runner or a CI agent runs under an account (
jenkins,gitlab-runner) that is not in thedockergroup, or was not restarted after being added. - Socket mounted into a container: with
-v /var/run/docker.sock:/var/run/docker.sock, the container user does not have the same GID as the host’sdockergroup. - Socket ownership changed: a
chownor a Snap install changed the socket’s group.
Step-by-step fix
-
Check the socket and your groups
Look at who owns the socket and whether
dockerappears in your groups. If the socket belongs to thedockergroup and that group is missing fromid -nG, it is a group problem.ls -l /var/run/docker.sock id -nG -
Add the user to the docker group (Linux)
This is the official post-install procedure. Warning: the
dockergroup grantsroot-equivalent rights on the machine (anyone can mount/into a container). Only grant it to trusted accounts.sudo groupadd docker 2>/dev/null || true sudo usermod -aG docker $USER -
Refresh the session and test
Log out and back in (or reboot the VM). For the current terminal only,
newgrp dockeropens a shell with the new group. Then test withoutsudo.newgrp docker docker run --rm hello-world -
Restore the socket permissions
If someone changed the socket, do not run
chmod 666orchmod 777: any local user would become root. Give it back to thedockergroup, or restart the daemon, which recreates the socket with the right permissions.sudo chown root:docker /var/run/docker.sock sudo systemctl restart docker -
CI and containers that use the socket
For Jenkins or GitLab Runner, add the service account to the group and restart the service. In a container that mounts the socket, add the host socket’s GID with
--group-add.sudo usermod -aG docker jenkins sudo systemctl restart jenkins docker run --rm \ -v /var/run/docker.sock:/var/run/docker.sock \ --group-add "$(stat -c '%g' /var/run/docker.sock)" \ docker:cli docker ps -
macOS / Windows: check the Docker Desktop context
Docker Desktop does not use a
dockergroup. If the error shows up, the CLI often points to the wrong socket. List the contexts and select the Docker Desktop one, then check thatDOCKER_HOSTis not set to a stale value.docker context ls docker context use desktop-linux echo "$DOCKER_HOST"
Did this fix work for you?
Confirm it in one click: it will rise for the next developers stuck on the same error.
Keep it from coming back
- Add the
usermod -aG dockerstep to your provisioning scripts (Ansible, cloud-init) for every account that needs Docker. - On a shared server, prefer rootless mode (
dockerd-rootless-setuptool.sh install): each user gets their own daemon without root rights. - Avoid mounting
/var/run/docker.sockinto containers: it is root access to the host. To build images in CI, tools such as rootless BuildKit or Kaniko avoid that mount.
Frequently asked questions
Why does it work with sudo but not without?
Because root can always open the socket. Without sudo, your user must belong to the docker group and the session must have been reopened after adding it.
Is chmod 666 /var/run/docker.sock a good fix?
No. Any local user could then drive Docker and gain root access to the machine. Use the docker group or rootless mode.
I added the group but the error remains, why?
The session has not been refreshed: log out completely (closing the terminal is not always enough with a desktop session or multiplexed SSH) or reboot. Check with id -nG.
Useful tools for this error
More Docker & DevOps errors
- Fix "Bind for 0.0.0.0:8080 failed: port is already allocated" (Docker)
- Fix "no space left on device" (Docker and Linux)
- Fix "exec format error" in Docker (arm64 / amd64)
- Fix "Cannot connect to the Docker daemon. Is the docker daemon running?"
- Fix "connect() failed (111: Connection refused) while connecting to upstream" (Nginx 502)
- Fix "exited with code 137" (OOMKilled) in Docker and Kubernetes
- Fix "CrashLoopBackOff" / "Back-off restarting failed container" (Kubernetes)