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.

Applies to:Docker Engine on Linuxnot version-specific
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 docker command as a normal user fails, for example docker ps, docker run hello-world or docker build.
  • sudo docker ps works.
  • After earlier use of sudo docker, a warning such as WARNING: Error loading config file: /home/user/.docker/config.json - stat ...: permission denied appears.

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 docker group 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 docker with sudo earlier can leave root-owned files in ~/.docker, which produces the config.json variant 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

Easy

Shows in seconds whether you are simply missing from the docker group.

  1. List the groups your current session belongs to.
    Terminal
    id -nG
  2. Look at the socket's owner, group and mode.
    Terminal
    ls -l /var/run/docker.sock
  3. If docker is missing from your groups, continue with the next fix.

2.Add your user to the docker group

EasyChanges settings

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.

  1. Create the group (skip if it already exists) and add your user.
    Terminalneeds sudo / root
    sudo groupadd docker
    sudo usermod -aG docker $USER
  2. Log out and log back in so the new membership is picked up. On a virtual machine, a restart may be needed.
  3. As a quicker alternative for the current shell only, run newgrp docker. It does not affect other terminals.
    Terminal
    newgrp docker
  4. Test it.
    Terminal
    docker run hello-world

3.Fix a root-owned ~/.docker folder

EasyChanges settings

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.

  1. Give yourself ownership back. This keeps everything in the folder.
    Terminalneeds sudo / root
    sudo chown "$USER":"$USER" /home/"$USER"/.docker -R
    sudo chmod g+rwx "$HOME/.docker" -R
  2. Alternatively, move the folder aside and let Docker recreate it. You will need to log in to registries again.
    Terminal
    mv ~/.docker ~/.docker.bak

4.Switch to rootless mode

AdvancedChanges settings

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.

  1. Make sure newuidmap and newgidmap are installed and that /etc/subuid and /etc/subgid contain at least 65,536 entries for your user.
  2. As your normal user, run the setup tool.
    Terminal
    dockerd-rootless-setuptool.sh install
  3. 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.
    Terminalneeds sudo / root
    sudo systemctl disable --now docker.service docker.socket
  4. Point the client at the rootless socket, replacing <UID> with your numeric user ID.
    Terminal
    export DOCKER_HOST=unix:///run/user/<UID>/docker.sock
  5. Verify that the client now works with docker info.
    Terminal
    docker info

Check that it worked

  • docker ps runs as your normal user without sudo and without a permission error.
  • docker run hello-world prints its welcome message.
  • The config.json ... permission denied warning no longer appears.

If it still fails

  • Run id -nG in a fresh login session and confirm docker is 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 docker works 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

  1. Linux post-installation steps for Docker EngineOfficial · Docker Inc.
  2. Rootless modeOfficial · Docker Inc.