184

When I try to run simple docker commands like:

$ docker ps -a

I get an error message:

Got permission denied ... /var/run/docker.sock: connect: permission denied

When I check permissions with

$ ls -al /var/run/

I see this line:

srw-rw---- root docker docker.sock

So, I follow an advice from many forums and add local user to docker group:

$ sudo usermod -aG docker $USER

But it does not help. I still get the very same error message. How can I fix it?

11
  • 9
    Did you re-login after making that change? The change is not available in the same session. Also does sudo docker ps work for you? Commented Feb 1, 2018 at 17:15
  • I open a new terminal and still get these error messages.
    – Jacobian
    Commented Feb 1, 2018 at 17:17
  • 1
    You have to restart the docker daemon, otherwise it won't let members of the docker group to control the docker daemon
    – Murmel
    Commented Feb 1, 2018 at 17:20
  • 3
    After changing users/groups you have to relogin, not just open new terminal.
    – Sergius
    Commented Feb 1, 2018 at 17:43
  • 1
    The oldest question in the world.
    – macosmi
    Commented Feb 13 at 22:20

15 Answers 15

329

For those new to the shell, the command:

$ sudo usermod -aG docker $USER

needs to have $USER defined in your shell. This is often there by default, but you may need to set the value to your login id in some shells.


Changing the groups of a user does not change existing logins, terminals, and shells that a user has open. To avoid performing a login again, you can simply run:

$ newgrp docker

to get access to that group in your current shell.


Once you have done this, the user effectively has root access on the server, so only do this for users that are trusted with unrestricted sudo access.

8
  • 1
    This did not work for me, but I was using namespaces. I had to use --userns=host. Commented Feb 12, 2019 at 19:56
  • I tried every other trick in this thread, followed the docs, reinstalled Docker to a newer version, rebooted plenty of times, everything I thought about. I am indeed in the docker group, but the default shell won't acknowledge it (maybe a problem with a script in my .profile?). Other than sudoing to the root user, only that newgrp command worked. Commented Feb 26, 2019 at 20:07
  • 4
    @BrunoLaturner If you are on Ubuntu, I've heard of LightDM causing an issue where it drops secondary groups from the login user.
    – BMitch
    Commented Feb 26, 2019 at 21:02
  • 3
    @BMitch are you in NSA spying me? That is my exact config and bug. Thanks! Solved following askubuntu.com/q/1057258/259660 Commented Feb 27, 2019 at 15:14
  • 4
    running newgrp docker command is necessary to activate the changes to groups
    – Jay Modi
    Commented Dec 6, 2019 at 17:38
109

Reason: The error message means that the current user can’t access the docker engine, because the user hasn't enough permissions to access the UNIX socket to communicate with the engine.

Quick Fix:

  1. Run the command as root using sudo.

    sudo docker ps
    
  2. Change the permissions of /var/run/docker.sock for the current user.

    sudo chown $USER /var/run/docker.sock
    

Caution: Running sudo chmod 777 /var/run/docker.sock will solve your problem but it will open the docker socket for everyone which is a security vulnerability as pointed out by @AaylaSecura. Hence it shouldn't be used, except for testing purposes on the local system.

Permanent Solution:

Add the current user to the docker group.

sudo usermod -a -G docker $USER

Note: You have to log out and log in again for the changes to take effect.

Refer to this blog to know more about managing Docker as a non-root user.

5
  • 8
    You're probably missing out on votes because people log out then forget to come back and upvote lols.
    – John Mee
    Commented Feb 7, 2020 at 7:51
  • 4
    I bet they're missing the upvotes cause the "Quick Fix" is a security disaster... The docker socket should never be accessible to world... Commented Apr 17, 2020 at 4:03
  • 1
    @AaylaSecura Yes, you're right. I had added it as a quick fix but again it's a bad practice. I have changed it in the answer now. Feel free to comment if you think It can be improved.
    – Nitish
    Commented Apr 18, 2020 at 5:37
  • 1
    this was the solution that worked for me... thanks!!!, the ownership of the docker.sock file was of root so no logout would ever fix it.
    – Carlos
    Commented Aug 9, 2020 at 14:15
  • 1
    I needed to restart the PC, for some reason logout and login did not work and I spend a lot of time troubleshooting this problem. Commented Aug 18, 2022 at 6:53
49
  1. Make sure your $USER variable is set

    $ echo $USER
    
    $ sudo usermod -aG docker $USER
    
  2. logout

  3. Upon login, restart the docker service

    $ sudo systemctl restart docker
    
    $ docker ps
    
5
  • 7
    Restarting the Docker daemon was a big one. Always forget to do that after adding user to Docker group :\ Commented Jul 29, 2019 at 23:45
  • 2
    There should be no need to restart the daemon, it's root, and already configured the socket to run as docker. The only thing I can think it fixes is if you modified the socket permissions.
    – BMitch
    Commented Jan 29, 2021 at 18:13
  • 1
    A Docker service restart solved the issue after adding the group to the OS environment. Thank you!
    – Artfaith
    Commented Sep 8, 2022 at 5:55
  • I had to restart the daemon after creating the docker group. Upvoting this answer.
    – Ruzihm
    Commented Dec 9, 2022 at 21:01
  • I actually had to reboot the computer, wtf. Logout and login did not help. newgrp worked, but re-login or sudo systemctl restart docker did NOT work in my case. weird, but in case somebody else wonders... Commented Mar 22, 2023 at 19:22
15

enter the command and explore docker without sudo command

sudo chmod 666 /var/run/docker.sock
2
  • 3
    Remember that anyone who can access the Docker socket can trivially root the entire host; running this command allows any local process to do that.
    – David Maze
    Commented Jun 13, 2022 at 13:54
  • Sadly, some operating systems keep changing permissions of this file back to its default :( Commented Jun 30, 2023 at 21:39
4

As mentioned earlier in the comment the changes won't apply until your re-login. If you were doing a SSH and opening a new terminal, it would have worked in new terminal

But since you were using GUI and opening the new terminal the changes were not applied. That is the reason the error didn't go away

So below command did do its job, its just a re-login was missed

sudo usermod -aG docker $USER
4

You need to manage docker as a non-root user. To create the docker group and add your user:

  1. Create the docker group.

    $ sudo groupadd docker

  2. Add your user to the docker group.

    $ sudo usermod -aG docker $USER

  3. Log out and log back in so that your group membership is re-evaluated.

If testing on a virtual machine, it may be necessary to restart the virtual machine for changes to take effect.

On a desktop Linux environment such as X Windows, log out of your session completely and then log back in.

On Linux, you can also run the following command to activate the changes to groups:

$ newgrp docker

  1. Verify that you can run docker commands without sudo.

    $ docker run hello-world

2

***Important Note on these answers: the docker group is not always "docker" sometimes it is "dockerroot", for example the case of Centos 7 installation by

sudo yum install -y docker

The first thing you should do, after installing Docker, is

sudo tail /etc/group

it should say something like

......
sshd:x:74:
postdrop:x:90:
postfix:x:89:
yourusername:x:1000:yourusername
cgred:x:996:
dockerroot:x:995:

In this case, it is "dockerroot" not "docker". So,

sudo usermod -aG dockerroot yourusername
logout
1
  • This is the correct answer for centos. There's no docker group. Commented Apr 21, 2023 at 20:17
2

When I try to run simple docker commands like: $ docker ps -a

I get an error message: Got permission denied ... /var/run/docker.sock: connect: permission denied.

[…] How can I fix it?

TL;DR: There are two ways (the first one, also mentioned in the question itself, was extensively addressed by other answers, but comes with security concerns; so I'll elaborate on this issue, and develop the second solution that can also be applicable for this fairly sensible use case).


Just to recall the context, the Docker daemon socket is owned by root:docker:

$ ls -l /var/run/docker.sock
srw-rw---- 1 root docker 0 janv. 28 14:23 /var/run/docker.sock

so with this default setup, one needs to prepend all docker CLI commands by sudo.

To avoid this, one can either:

  1. add one's user account ($USER) to the docker group − but that's quite risky to do this on one's personal workstation, as this would amount to provide all programs run by the user with root permissions without any sudo password prompt nor auditing.

    See also:

  2. one can otherwise prepend sudo automatically without typing sudo docker manually: to this aim, a solution consists in adding the following alias in the ~/.bashrc (see e.g. this thread for details):

    __docker() {
        if [[ "${BASH_SOURCE[*]}" =~ "bash-completion" ]]; then
            docker "$@"
        else
            sudo docker "$@"
        fi
    }
    alias docker=__docker
    

    Then one can test this by opening a new terminal and typing:

    docker run --pul〈TAB〉 # → docker run --pull
                           # autocompletion works
    docker run --pull always --rm -it debian:11  # ask one's password
    \docker run --help  # bypass the alias (thanks to the \) and ask no password
    
2
  • @SridharSarnobat I rollbacked your edit since running sudo chmod a+rx /var/run/docker.sock is definitely not a summary of my answer, nor a proper solution…
    – ErikMD
    Commented Jan 3, 2022 at 16:18
  • I don't know why I got a downvote: I sincerely believe that adding a mere .bashrc alias as I propose in my answer is a better trade-off than the currently accepted solution, because (1) it's safer from a security perspective (no user process can sneakily become root because of Docker's daemon attack surface), and (2) it achieves the same goal: we can just write docker run -it ubuntu or so in one's terminal…
    – ErikMD
    Commented Jan 3, 2022 at 20:44
1

As my user is and AD user, I have to add the AD user to the local group by manually editing /etc/group file. Unforrtunately the adduser commands do not seem to be nsswitch aware and do not recognize a user not locally defined when adding someone to a group.

Then reboot or refresh /etc/group. Now, you can use docker without sudo.

Regards.

1

With the help of the below command I was able to execute the docker command without sudo

sudo setfacl -m user:$USER:rw /var/run/docker.sock

1

I do all things but still have the error. To solve it, your user must must read/write in /var/run/docker.sock:

$ ls -al /var/run/ |grep docker
srw-rw----  1 root docker    0 Mar  3 10:07 docker.sock

So you can edit /etc/sudoers to give you user root permisions, or you can modify the group of /var/run/docker.sock to change to docker group:

$ chgrp docker /var/run/docker.sock
$ ls -al /var/run/ |grep docker
srw-rw----  1 root docker    0 Mar  3 10:07 docker.sock

Now you can do docker ps without sudo. Do not recomended to change docker.sock permissions

0

If you are using any node.js process that triggers the docker run command, then you can use:

sudo node index.js

If you are using pm2 that starts the node.js process/server that in turn triggers the docker run command, then use:

sudo pm2 start index.js
-2

You have to use pns executer instead of docker. run the following patch which modifies the configmap and you are all set.

kubectl -n argo patch cm workflow-controller-configmap -p '{"data": {"containerRuntimeExecutor": "pns"}}' ;

ref: https://www.youtube.com/watch?v=XySJb-WmL3Q&list=PLGHfqDpnXFXLHfeapfvtt9URtUF1geuBo&index=2&t=3996s

-3

bash into container as root user docker exec -it --user root <dc5> bash

create docker group if it's not already created groupadd -g 999 docker

add user to docker group usermod -aG docker jenkins

change permissions chmod 777 /var/run/docker.sock

1
  • 2
    I strongly recommend against changing the permissions on docker.sock. This gives every user and process on the host full root access without a password and minimal logging of their actions.
    – BMitch
    Commented Jan 3, 2022 at 21:10
-3

Run below command in docker server, your problem will be solved

sudo chmod 777 /var/run/docker.sock

1
  • Making a socket file permission 777 is not the smartest thing to do. It's a massive security risk. Some OS's also base this on the permissions of the folder. And not the file.
    – Codixer
    Commented Apr 10 at 12:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.