Docker says permission denied when your user runs a command
permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sockShort 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.
Fix it fast
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 id2Match what you see, then jump to its fix
- 1
id does not list docker, and sudo docker ps worksYour user is not in the docker groupFix - 2
getent shows your user in docker, but id in your current shell does notThe group was added but the session is oldFix - 3
WARNING: Error loading config file ... permission denied~/.docker was created by root through earlier sudo useFix
- 1
What does the error look like?
permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sockGot permission denied while trying to connect to the Docker daemon socketWARNING: 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 docker2Fix
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 docker3Verify
Docker now runs without sudo.
docker run hello-worldConfirmed by docs.docker.com
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 docker2Fix
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 jenkins3Verify
Run the command as that account and check it lists docker.
sudo -u jenkins docker psConfirmed by docs.docker.com
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 ~/.docker2Fix
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" -R3Verify
The warning no longer appears.
docker psConfirmed by docs.docker.com
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 fixServers
Apache logs SIGWINCH and exits on its own
the httpd container stops although nothing crashed
SIGWINCH is Apache's graceful-stop signal.
2 causes, each with a fixServers
sam local fails: Running AWS SAM projects locally requires Docker
Cannot connect to the Docker daemon. Is the docker daemon running on this host?
sam local start-api and sam local invoke run your function inside a Docker container, so SAM stops if it cannot talk to Docker.
3 causes, each with a fixServers
dnf fails to download repo metadata in a CentOS 8 Docker build
The same Dockerfile built fine before 2022
CentOS Linux 8 reached end of life on 31 December 2021 and its content left the mirrors.
2 causes, each with a fixServers
pip install confluent-kafka fails because librdkafka/rdkafka.h is missing
pip stops while building confluent-kafka from source
The error means pip is compiling confluent-kafka from source, and the compiler cannot find the librdkafka C headers.
3 causes, each with a fixServers
Cypress says the npm package is installed but the binary is missing
Cypress binary is missing after npm install in CI or in a Docker build
The npm package is only a launcher.
3 causes, each with a fix