Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

permission denied while trying to connect to the Docker daemon socket

The socket exists and the kernel refused the connection. Group membership, stale login shells, rootless paths and SELinux are the real causes.

~5 min readDocker
#Docker#Linux#Permissions#Troubleshooting
A Docker socket plug blocked by a cyan permission gate, an amber group key hanging beside it

Introduction & Quick Summary

text
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/version": dial unix /var/run/docker.sock: connect: permission denied

The client reached the socket file and the kernel refused the connection because your user lacks permission on that inode. This is an operating-system permission problem, not a Docker problem: the daemon is running, the socket exists, and the CLI is fine.

Four things to check, in order:

  • You are not in the docker group. The socket is usually srw-rw---- root docker, so membership is the intended access path.
  • Your shell has not picked up the new group. usermod -aG docker $USER affects future logins only; the current session still carries the old group list.
  • The socket lives somewhere else. Rootless Docker uses $XDG_RUNTIME_DIR/docker.sock, Colima and Rancher Desktop use a per-user path, and a stray DOCKER_HOST points the client at the wrong file.
  • Mandatory access control is blocking it. SELinux or AppArmor can deny access even with correct group membership, and on some hosts the socket context was changed by a manual chmod.
SymptomDistinguishing testFix
Fails as user, works as rootsudo docker version succeedsJoin docker group, re-login
Group listed but still deniedid -nG lacks dockerStart a new login shell
dial unix ...: no such filePath wrong, not permissionCheck docker context ls
Works in IDE, fails in terminalDifferent shells load different envCheck DOCKER_HOST per shell
Works until rebootRootless socket path changesExport DOCKER_HOST in profile

Core Mechanics, Signatures & Reproducible Setup

The daemon listens on a Unix socket owned by root:docker with mode 0660 by default. Unix socket permissions are enforced by the kernel at connect(), so group membership is what grants access. Socket activation through systemd keeps the socket at /run/docker.sock (a symlink target of /var/run/docker.sock), and its ownership is re-applied on each start.

Inspect the actual state rather than trusting a guide:

bash
ls -l /var/run/docker.sock stat -c '%U:%G %a %n' /var/run/docker.sock id -nG getent group docker docker context ls echo "DOCKER_HOST=$DOCKER_HOST"

A correct, minimal setup on a Debian-like host:

sudo groupadd -f docker sudo usermod -aG docker "$USER" newgrp docker # applies to this shell only docker run --rm hello-world

newgrp docker opens a subshell with the new group so you do not have to log out; a full re-login is still required for IDE terminals, tmux servers and SSH sessions started before the change.

For the rootless case the socket is not in /var/run at all:

bash
export DOCKER_HOST="unix://$XDG_RUNTIME_DIR/docker.sock" docker context use rootless docker info --format '{{.SecurityOptions}}'

A quick reproduction of the exact failure uses a fresh user with no supplementary groups:

bash
sudo useradd -m tester sudo -u tester docker ps # permission denied while trying to connect to the Docker daemon socket sudo usermod -aG docker tester sudo -u tester -i docker ps # still cached? use a login shell

Key Comparison Table

Access modelSocket pathOwner:groupSurvives reboot
Rootful, systemd socket/var/run/docker.sockroot:dockerYes, recreated with same mode
Rootful, manual dockerd/var/run/docker.sockSet at startDepends on unit file
Rootless$XDG_RUNTIME_DIR/docker.sockYour userPath changes per session
Docker Desktop Linux~/.docker/desktop/docker.sockYour userYes
Colima~/.colima/default/docker.sockYour userPer profile
Remote TCP with TLStcp://host:2376n/aRequires certs
FixScopeSecurity effectPersistence
Add user to docker groupWhole user accountEquivalent to root on the hostPermanent
sudo dockerSingle commandRuns the client as rootNone
chmod 666 the socketAll local usersRemoves access controlUntil daemon restart
Rootless DockerYour user onlySmallest blast radiusPer session or linger
DOCKER_HOST to a VMSingle projectIsolated daemonWherever exported

Practical Edge Cases, Error Fixes & Deep Dive Code Examples

Case 1: CI runners that recreate the user every job

A containerized CI job usually already runs as root; a self-hosted runner does not. Add the group in the runner's shell profile rather than in the pipeline, and remember that the docker group must exist before usermod runs:

bash
sudo groupadd -f docker sudo usermod -aG docker "$(id -un)" exec sudo -u "$(id -un)" -i bash -lc 'docker info >/dev/null'

Case 2: IDE and GUI sessions with stale groups

VS Code, JetBrains IDEs and desktop launchers inherit the group list from the session manager at login. After changing membership, log out of the desktop session, not just the terminal. Verify from inside the IDE's integrated terminal with id -nG.

Case 3: The socket exists but the daemon is not running

bash
sudo systemctl status docker.socket docker.service journalctl -u docker.service -n 50 --no-pager

A stopped daemon yields connect: no such file or directory on some hosts and this permission error on others when systemd socket activation holds a stale path. Restart the socket unit first, then the service.

Case 4: Podman's Docker compatibility socket

podman.socket provides /run/user/$UID/podman/podman.sock, and symlinking it to /var/run/docker.sock is a common shortcut that produces confusing permission errors later. Point the client explicitly instead:

bash
export DOCKER_HOST="unix:///run/user/$(id -u)/podman/podman.sock" docker context create podman --docker "host=$DOCKER_HOST" docker context use podman

Case 5: SELinux denials on Fedora and RHEL

bash
sudo ausearch -m avc -ts recent | grep docker ls -Z /var/run/docker.sock

If the context is not docker_var_run_t or an equivalent, restore labeling rather than widening permissions:

bash
sudo restorecon -v /var/run/docker.sock

Case 6: Testcontainers and language SDKs

Libraries read the environment, not your shell history. A test that fails in Maven or Gradle but passes with docker run is usually reading a different socket. Configure it explicitly:

java
DockerClientFactory.instance().client(); // Testcontainers // or // TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE=/run/user/1000/docker.sock

Common Compilation/Runtime Mistakes & Version Gotchas

  • sudo docker creates root-owned files. Containers mounting a bind volume write files as root, and the next non-root build fails on permission. Use --user $(id -u):$(id -g) or fix ownership after the fact.
  • The docker group is root-equivalent. Anyone in it can mount the host filesystem into a container. On shared machines, rootless Docker or a remote builder is the safer arrangement.
  • chmod 666 is not a fix. It survives only until the daemon restarts and silently grants every local process full control of the host.
  • Docker Desktop on Linux. It uses its own socket under ~/.docker/desktop/ and a context named desktop-linux, so host instructions about systemctl enable docker do not apply and usermod changes nothing.
  • WSL2. The distribution needs Docker integration enabled in Docker Desktop settings; group membership inside WSL is irrelevant because the socket is a Windows-side proxy.
  • Socket path length in DOCKER_HOST. unix:// plus a long home path can exceed the 108-byte sun_path limit and produce an error that reads like a permission failure.

When the error is wrapped inside a long stack trace from a build tool, extract the daemon response and inspect it with the JSON formatter if the API returned structured output; docker info --format '{{json .SecurityOptions}}' is a quick way to confirm whether a rootless or SELinux profile is active. If you need to reproduce the daemon call by hand, the curl generator can build the --unix-socket request. For checking the port and socket path a Compose file expects, the Docker Compose viewer validates the file before a docker compose up fails for an unrelated reason.

Primary references: Docker's post-install steps for Linux cover group membership and its security implications, and rootless mode documents the socket path and setup for non-root daemons.

Official references

primary documentation

The Docker documentation that defines the behaviour this guide explains.

Try matching browser tools

All tools

Small utilities that fit Docker work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Docker guides

More in Docker

All Docker guides

The guides filed next to this one in Docker, in library order.