Docker "permission denied while trying to connect to the Docker daemon socket": how to fix it
The Docker daemon is running, but your Linux user isn't allowed to open its socket at `/var/run/docker.sock`. The usual fix is to add your user to the `docker` group, then log out and back in.
On this page
What it means
The full message reads permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: ... connect: permission denied. Older clients print it as "Got permission denied while trying to connect...". It means the docker command found the daemon's socket file, but the operating system refused to let your user open it.
The daemon itself is normally fine. It runs as root, and per Docker's post-install documentation the socket is accessible to members of the docker group. If you aren't in that group, every command fails, even though sudo docker ps works.
This is a different problem from "Cannot connect to the Docker daemon. Is the docker daemon running?", which points to a stopped daemon or a wrong DOCKER_HOST. For that one, see Cannot connect to the Docker daemon. For general permission errors in the shell, see Permission denied in the Linux shell.
Symptoms and causes
You are likely dealing with this problem if:
- Any
dockercommand as a normal user fails, for exampledocker ps,docker run hello-worldordocker build. sudo docker psworks.- After earlier use of
sudo docker, a warning such asWARNING: Error loading config file: /home/user/.docker/config.json - stat ...: permission deniedappears.
The usual causes, most common first:
- Your user is not in the docker group
- This is the usual case on a fresh install. Only root and members of the
dockergroup can open the socket. - The group change hasn't reached your session
- You were added to the group, but group membership is read at login, so the current session doesn't have it yet.
- ~/.docker is owned by root
- Running
dockerwithsudoearlier can leave root-owned files in~/.docker, which produces theconfig.jsonvariant of the error. - The socket's owner, group or mode was changed
- Check it with
ls -l /var/run/docker.sock. - The docker group doesn't exist
- Some install methods don't create it, so there is nothing to add your user to.
How to fix it
Work through these in order. They start with the quickest, safest checks and move to deeper repairs; stop as soon as the problem is gone.
1.Check your groups and the socket
Shows in seconds whether you are simply missing from the docker group.
- List the groups your current session belongs to.
id -nG - Look at the socket's owner, group and mode.
ls -l /var/run/docker.sock - If
dockeris missing from your groups, continue with the next fix.
2.Add your user to the docker group
This is the method Docker documents for running the client without sudo.
Before you start: Docker's documentation states that the docker group grants root-level privileges to the user. Only add accounts you trust with that level of access.
- Create the group (skip if it already exists) and add your user.
sudo groupadd docker sudo usermod -aG docker $USER - Log out and log back in so the new membership is picked up. On a virtual machine, a restart may be needed.
- As a quicker alternative for the current shell only, run
newgrp docker. It does not affect other terminals.newgrp docker - Test it.
docker run hello-world
3.Fix a root-owned ~/.docker folder
Fixes the config.json ... permission denied warning caused by earlier sudo docker use.
Before you start: Deleting ~/.docker can also remove your registry login credentials, CLI plugins (such as compose and buildx), contexts (including a rootless context) and buildx builders. Prefer the chown option, which loses nothing. If you want a fresh folder, rename it instead of deleting it, so you can restore it. rm without sudo may fail or only partly work if root-owned files are inside.
- Give yourself ownership back. This keeps everything in the folder.
sudo chown "$USER":"$USER" /home/"$USER"/.docker -R sudo chmod g+rwx "$HOME/.docker" -R - Alternatively, move the folder aside and let Docker recreate it. You will need to log in to registries again.
mv ~/.docker ~/.docker.bak
4.Switch to rootless mode
Docker's rootless mode (Docker 20.10 or later) runs the daemon as your own user, so the root-equivalent docker group isn't needed.
Before you start: Rootless mode uses a different socket, and existing containers and images from the system daemon will not appear in it. Read the official rootless page before switching.
- Make sure
newuidmapandnewgidmapare installed and that/etc/subuidand/etc/subgidcontain at least 65,536 entries for your user. - As your normal user, run the setup tool.
dockerd-rootless-setuptool.sh install - Optional: disable the system-wide daemon. This stops every container managed by the system daemon, so do not do it on shared or production hosts.
sudo systemctl disable --now docker.service docker.socket - Point the client at the rootless socket, replacing
<UID>with your numeric user ID.export DOCKER_HOST=unix:///run/user/<UID>/docker.sock - Verify that the client now works with
docker info.docker info
Check that it worked
docker psruns as your normal user without sudo and without a permission error.docker run hello-worldprints its welcome message.- The
config.json ... permission deniedwarning no longer appears.
If it still fails
- Run
id -nGin a fresh login session and confirmdockeris listed. If it isn't, the group change hasn't applied yet. - If the message is "Cannot connect to the Docker daemon. Is the docker daemon running?" instead, follow Cannot connect to the Docker daemon.
- Using
sudo dockerworks as a stopgap, but Docker documents that it can leave root-owned files in~/.docker, so don't rely on it long term.
Questions
Why does it still fail right after usermod?
Group membership is read at login. Log out and back in, or use newgrp docker for the current shell.
Sources
- Linux post-installation steps for Docker EngineOfficial · Docker Inc.
- Rootless modeOfficial · Docker Inc.