How to fix docker: Got permission denied while trying to connect to the Docker daemon socket

Better Stack Team
Updated on November 16, 2022

This error may appear when running docker commands and it is caused by insufficient privilages.


To resolve the problem, you need to do the following:

  1. Create a docker group
sudo groupadd docker
  1. Add your user to the docker group
sudo usermod -aG docker ${USER}
  1. You would need to log out and log back in so that your group membership is re-evaluated or type the following command:
su -s ${USER}
  1. • Verify that you can run docker commands without sudo
docker run hello-world
  • This command downloads a test image and runs it in a container. When the container runs, it prints an informational message and exits.
  • If you initially ran Docker CLI commands using sudo before adding your user to the docker group, you may see the following error, which indicates that your ~/.docker/ directory was created with incorrect permissions due to the sudo commands.
WARNING: Error loading config file: /home/user/.docker/config.json -
stat /home/user/.docker/config.json: permission denied

• To fix this problem, either remove the ~/.docker/ directory (it is recreated automatically, but any custom settings are lost), or change its ownership and permissions using the following commands:

sudo chown "$USER":"$USER" /home/"$USER"/.docker -R
sudo chmod g+rwx "$HOME/.docker" -R
Got an article suggestion? Let us know
Explore more
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

We are hiring.

Software is our way of making the world a tiny bit better. We build tools for the makers of tomorrow.

Explore all positions →