Question

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

I’ve just installed docker but I have to run it with sudo every time. If I don’t add sudo I get the following error:

Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.40/containers/json: dial unix /var/run/docker.sock: connect: permission denied

Is there a way around that? I want to be able to run docker without having to type my password each time…

Show comments

Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
August 12, 2019
Accepted Answer

Hello,

According to the official Docker docs here:

https://docs.docker.com/install/linux/linux-postinstall/#manage-docker-as-a-non-root-user

You need to do the following:

To create the docker group and add your user:

  • Create the docker group.
sudo groupadd docker
  • Add your user to the docker group.
sudo usermod -aG docker ${USER}
  • You would need to loog out and log back in so that your group membership is re-evaluated or type the following command:
su -s ${USER}
  • 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

Here’s also a quick video demo on how to do this:

Hope that this helps! Regards, Bobby

The above is almost right, but opens up a security gap that let’s everyone get access to docker.sock

Instead of sudo chmod 666 /var/run/docker.sock which opens it to everyone, enter

sudo chown root:docker /var/run/docker.sock

That way root still has it’s connection with docker but anyone in the docker group gets access too.

If you run into this:

ubuntu@ip-x-x-x-x:~$ docker login -u username -p password

WARNING! Using --password via the CLI is insecure. Use --password-stdin.
Error saving credentials: error storing credentials - err: exit status 1, out: `Cannot autolaunch D-Bus without X11 $DISPLAY`

Try this: sudo apt install gnupg2 pass

See this github answer for details: https://github.com/docker/cli/issues/1136#issuecomment-459649905

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.