Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

Docker says permission denied when your user runs a command

Errorpermission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock

Short answer

Your user is not allowed to open /var/run/docker.sock. Add the user to the docker group, then start a new login session or run newgrp docker so the membership applies. For Jenkins or CI, add the service account the same way and restart the agent. Never use chmod 666 on the socket.

  • Applies to Docker Engine 28, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    See who owns the socket and whether your account is in that group. The id output must list docker.

    ls -l /var/run/docker.sock
    id
  2. 2Match what you see, then jump to its fix

    1. 1id does not list docker, and sudo docker ps worksYour user is not in the docker groupFix
    2. 2getent shows your user in docker, but id in your current shell does notThe group was added but the session is oldFix
    3. 3WARNING: Error loading config file ... permission denied~/.docker was created by root through earlier sudo useFix

What does the error look like?

  • permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock
  • Got permission denied while trying to connect to the Docker daemon socket
  • WARNING: Error loading config file: /home/user/.docker/config.json - stat /home/user/.docker/config.json: permission denied

Cause 1 of 3

Your user is not in the docker group

1Check

The socket belongs to root and the docker group. Only members of that group can use it without sudo.

getent group docker

2Fix

Create the group if it is missing, add your user, and activate the membership in the current shell.

sudo groupadd docker
sudo usermod -aG docker $USER
newgrp docker

3Verify

Docker now runs without sudo.

docker run hello-world

Confirmed by docs.docker.com

Did this fix it?

Cause 2 of 3

The group was added but the session is old

1Check

Group membership is read at login, so a shell, an IDE or a service started earlier keeps the old list.

id
getent group docker

2Fix

Log out and back in. For a service account such as the one running Jenkins or a CI agent, add it to the group and restart that service.

sudo usermod -aG docker jenkins
sudo systemctl restart jenkins

3Verify

Run the command as that account and check it lists docker.

sudo -u jenkins docker ps

Confirmed by docs.docker.com

Did this fix it?

Cause 3 of 3

~/.docker was created by root through earlier sudo use

1Check

Running docker with sudo before the group fix can leave ~/.docker owned by root.

ls -ld ~/.docker

2Fix

Give the directory back to your user. Removing it also works, but you lose its settings.

sudo chown "$USER":"$USER" /home/"$USER"/.docker -R
sudo chmod g+rwx "$HOME/.docker" -R

3Verify

The warning no longer appears.

docker ps

Confirmed by docs.docker.com

Did this fix it?

How do I stop it coming back?

  • Treat docker group membership as root access: add only accounts you would trust with root.
  • Do not mix sudo docker and plain docker in the same account.
  • For a service account, add the group when you create the account and restart the service after changes.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Servers

The docker command cannot reach the Docker daemon

Cannot connect to the Docker daemon at unix:///Users/<user>/.docker/run/docker.sock. Is the docker daemon running?

This error means the docker client could not talk to a running daemon.

4 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.