Question

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

Posted August 12, 2019 777.6k views
Linux BasicsUbuntuDocker

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..

1 comment

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.

×
7 answers

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

  • I also had to restart the docker daemon to get things working:

    sudo systemctl restart docker
    
    • hello,pls i having the permission denied… issue applying your solution and getting to this step ‘su -s ${USER}’, its asking me for a password: . which the only password i can think of is my laptop password and inputting that . it says su: Authentication failure.
      pls i dont know what is wrong, this is my first time using the docker

      • The password su ${USER} requires you is the one for $USER, which will expand to the current user. You might as well just log out and log in again.

        I think the author meant su ${USER} not su -s ${USER}, since -s is used to specify a shell.

After an upgrade I got the permission denied. Doing the steps of ‘mkb’ post install steps don’t have change anything because my user was already in the 'docker’ group; I retry-it twice any way without success.

After an search hour this following solution finaly worked :

sudo chmod 666 /var/run/docker.sock

While doing production config i got the permission issue.I tried below solution to resolve the issue.

Error Message**

    ubuntu@node1:~$ docker run hello-world
    docker: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post http://%2Fvar%2Frun%2Fdocker.sock/v1.38/containers/create: dial unix /var/run/docker.sock: connect: permission denied.
    See 'docker run --help'.

Solution:permissions of the socket indicated in the error message, /var/run/docker.sock:

    ubuntu@ip-172-31-21-106:/var/run$ ls -lrth docker.sock
    srw-rw---- 1 root root 0 Oct 17 11:08 docker.sock
    ubuntu@ip-172-31-21-106:/var/run$ sudo chmod 666 /var/run/docker.sock
    ubuntu@ip-172-31-21-106:/var/run$ ls -lrth docker.sock
    srw-rw-rw- 1 root root 0 Oct 17 11:08 docker.sock

After changes permission for docket.sock then execute below command to check permissions.

ubuntu@ip-172-31-21-106:/var/run$ docker run hello-world
    Unable to find image 'hello-world:latest' locally
    latest: Pulling from library/hello-world
    1b930d010525: Pull complete
    Digest: sha256:c3b4ada4687bbaa170745b3e4dd8ac3f194ca95b2d0518b417fb47e5879d9b5f
    Status: Downloaded newer image for hello-world:latest

    Hello from Docker!
    This message shows that your installation appears to be working correctly.

    To generate this message, Docker took the following steps:
     1. The Docker client contacted the Docker daemon.
     2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
        (amd64)
     3. The Docker daemon created a new container from that image which runs the
        executable that produces the output you are currently reading.
     4. The Docker daemon streamed that output to the Docker client, which sent it
        to your terminal.

    To try something more ambitious, you can run an Ubuntu container with:
     $ docker run -it ubuntu bash

    Share images, automate workflows, and more with a free Docker ID:
     https://hub.docker.com/

    For more examples and ideas, visit:
     https://docs.docker.com/get-started/

can’t help but ask how many of you have simply run

$ newgrp docker

after adding your id to the docker group? It works as expected. In many cases you may find the primary user group vs additional groups may be an impacting condition in how the scripts and code works.

  • Issue :

    $ docker version

    Got this 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/version: dial unix /var/run/docker.sock: connect: permission denied

    Solution: as a non-root user run he below commands.

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

    Actually I missed this command($ newgrp docker)… after running it worked like a cham. Thanks to “rondemena”. Changing permissions i didn’t think so it is a correct option.

Show answer This answer has been marked as resolved by AHA.
Show answer This answer has been marked as resolved by szotrj.

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

Submit an Answer