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

Better Stack Team
Updated on November 9, 2023

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

Solution

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.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github