Docker "permission denied" on /var/run/docker.sock: how to fix it
The Docker command line can't open the daemon's socket, `/var/run/docker.sock`, because your user is neither root nor in the `docker` group. Adding yourself to the `docker` group and starting a fresh login session usually fixes it. Be aware that this group gives root-level power over the machine.
On this page
What it means
Docker works in two parts. A background service, the daemon, runs as root. The docker command you type is a client that talks to it through a Unix socket file at /var/run/docker.sock.
If your account is not root and not in the docker group, the client is refused at the socket and reports permission denied for the Docker daemon socket. Typically sudo docker ps works while plain docker ps does not. That is the sign this article applies.
Symptoms and causes
You are likely dealing with this problem if:
docker psordocker runfails with permission denied on/var/run/docker.sock, whilesudo docker ...works.WARNING: Error loading config file: /home/user/.docker/config.json - stat /home/user/.docker/config.json: permission denied, often after earlier use ofsudo docker.- The error keeps coming back in an IDE terminal or CI job even after you changed something.
The usual causes, most common first:
- Your user is not in the docker group
- This is the main cause. Only root and members of the
dockergroup can open the socket. - The group was added but the session is old
- Group membership is read at login. Shells, IDE terminals and services that were already running (a CI agent, for example) don't see the new group until they are restarted or you log in again.
- ~/.docker is owned by root
- Running
sudo dockerearlier can create or change~/.dockeras root, so your normal user can't readconfig.json. - DOCKER_HOST or the Docker context points elsewhere
- A rootless or remote daemon will not respond to fixes on the system socket. Check which one the client is using.
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.Add your user to the docker group
This is the fix Docker documents for the most common cause.
Before you start: Docker states that the docker group grants root-level privileges to the user. Only add accounts you trust. If that is too much access, use the rootless mode solution below instead.
- Create the group (it may already exist, which is fine) and add your user.
sudo groupadd docker sudo usermod -aG docker $USER - Log out and back in. If you are on a virtual machine, restarting it also works. To apply the group to the current shell only, run
newgrp docker(it starts a child shell).newgrp docker - Check that the group is now listed.
groups - Test with the hello-world image.
docker run hello-world
2.Fix ownership of ~/.docker
Use this when the warning mentions config.json in your home directory, usually after running Docker with sudo.
Before you start: The alternative of deleting ~/.docker also removes custom settings and stored credentials. Fixing ownership keeps them.
- Give the directory back to your user.
sudo chown "$USER":"$USER" /home/"$USER"/.docker -R sudo chmod g+rwx "$HOME/.docker" -R - Run
docker psagain and confirm the warning is gone.
3.Check DOCKER_HOST and the Docker context
If the client is aimed at a remote or rootless daemon, changing the system socket does nothing.
- Print the environment variable and list contexts.
echo $DOCKER_HOST docker context ls - If you set up rootless mode, a
rootlesscontext, or aDOCKER_HOSTyou set deliberately for it, is expected: leave it and troubleshoot the rootless daemon instead. IfDOCKER_HOSTor the selected context is something you didn't intend, unset the variable or switch to the context you meant to use, then retry.
4.Use rootless mode instead
Rootless mode runs the daemon as your own user, so you don't need the root-equivalent docker group. It needs Docker 20.10 or later.
Before you start: This replaces the system daemon for your user. Images and containers from the old daemon will not appear in the rootless one.
- Make sure
uidmap(newuidmapandnewgidmap) is installed and that/etc/subuidand/etc/subgidgive your user at least 65,536 subordinate IDs. - Optionally stop and disable the system daemon first.
sudo systemctl disable --now docker.service docker.socket - Run the setup tool, which comes from the
docker-ce-rootless-extraspackage. It normally creates arootlessDocker CLI context and switches to it, so you usually don't need to set anything else.dockerd-rootless-setuptool.sh install - Run
docker infoand check that the context isrootless.docker info - Only if you are not using the
rootlesscontext, point the client at the rootless socket withDOCKER_HOSTinstead, using your own UID in place of 1000. This is an alternative to the context, not an extra step.export DOCKER_HOST=unix:///run/user/1000/docker.sock - To keep the daemon running after you log out and at boot, enable lingering.
sudo loginctl enable-linger <user>
Check that it worked
docker run hello-worldcompletes without a permission error and withoutsudo.groupsoridlistsdockerin the shell you are using.- No
config.jsonpermission warning appears when you rundocker ps.
If it still fails
- Use
sudo docker ...only as a temporary workaround, and avoid making it a habit: it is what causes the~/.dockerownership problem. - Do not run
chmod 666 /var/run/docker.sock. It lets every local user control the daemon. - Make sure the shell you test in is new. IDE terminals, existing SSH sessions and CI agents need a fresh login or restart to see a new group.
- If you use Docker Desktop on Mac or Windows, the socket is set up differently and these steps may not apply. If a container (not the client) is being refused on a mounted volume or device, that is a separate problem not covered here.
Questions
Is adding myself to the docker group safe?
It is convenient but not low-risk. Docker says the group grants root-level privileges, and bind mounts let a container change the host filesystem. Add only trusted users, or use rootless mode.
Why does it still fail right after usermod?
Group membership is only read at login. Log out and in again, restart the VM, or run newgrp docker for the current shell.
Sources
- Post-installation steps for LinuxOfficial · Docker Inc.
- Rootless modeOfficial · Docker Inc.
- Docker Engine security (daemon attack surface)Official · Docker Inc.