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.

Applies to:Docker Engine on Linux
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.

Flow from docker command to the socket, blocked for non-group users, to the root daemon
IllustrationThe client is stopped at the socket file, so the daemon never receives the command.

Symptoms and causes

You are likely dealing with this problem if:

  • docker ps or docker run fails with permission denied on /var/run/docker.sock, while sudo docker ... works.
  • WARNING: Error loading config file: /home/user/.docker/config.json - stat /home/user/.docker/config.json: permission denied, often after earlier use of sudo 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 docker group 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 docker earlier can create or change ~/.docker as root, so your normal user can't read config.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

EasyChanges settings

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.

  1. Create the group (it may already exist, which is fine) and add your user.
    Terminal
    sudo groupadd docker
    sudo usermod -aG docker $USER
  2. 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).
    Terminal
    newgrp docker
  3. Check that the group is now listed.
    Terminal
    groups
  4. Test with the hello-world image.
    Terminal
    docker run hello-world

2.Fix ownership of ~/.docker

EasyChanges settings

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.

  1. Give the directory back to your user.
    Terminal
    sudo chown "$USER":"$USER" /home/"$USER"/.docker -R
    sudo chmod g+rwx "$HOME/.docker" -R
  2. Run docker ps again and confirm the warning is gone.

3.Check DOCKER_HOST and the Docker context

Easy

If the client is aimed at a remote or rootless daemon, changing the system socket does nothing.

  1. Print the environment variable and list contexts.
    Terminal
    echo $DOCKER_HOST
    docker context ls
  2. If you set up rootless mode, a rootless context, or a DOCKER_HOST you set deliberately for it, is expected: leave it and troubleshoot the rootless daemon instead. If DOCKER_HOST or 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

AdvancedChanges settings

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.

  1. Make sure uidmap (newuidmap and newgidmap) is installed and that /etc/subuid and /etc/subgid give your user at least 65,536 subordinate IDs.
  2. Optionally stop and disable the system daemon first.
    Terminalneeds sudo / root
    sudo systemctl disable --now docker.service docker.socket
  3. Run the setup tool, which comes from the docker-ce-rootless-extras package. It normally creates a rootless Docker CLI context and switches to it, so you usually don't need to set anything else.
    Terminal
    dockerd-rootless-setuptool.sh install
  4. Run docker info and check that the context is rootless.
    Terminal
    docker info
  5. Only if you are not using the rootless context, point the client at the rootless socket with DOCKER_HOST instead, using your own UID in place of 1000. This is an alternative to the context, not an extra step.
    Terminal
    export DOCKER_HOST=unix:///run/user/1000/docker.sock
  6. To keep the daemon running after you log out and at boot, enable lingering.
    Terminalneeds sudo / root
    sudo loginctl enable-linger <user>

Check that it worked

  • docker run hello-world completes without a permission error and without sudo.
  • groups or id lists docker in the shell you are using.
  • No config.json permission warning appears when you run docker ps.

If it still fails

  • Use sudo docker ... only as a temporary workaround, and avoid making it a habit: it is what causes the ~/.docker ownership 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

  1. Post-installation steps for LinuxOfficial · Docker Inc.
  2. Rootless modeOfficial · Docker Inc.
  3. Docker Engine security (daemon attack surface)Official · Docker Inc.