159

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?

Jacobian
  • 10,122
  • 29
  • 128
  • 221
  • 6
    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? – Tarun Lalwani Feb 01 '18 at 17:15
  • I open a new terminal and still get these error messages. – Jacobian Feb 01 '18 at 17:17
  • `sudo docker ps` works. But I need to work with docker under my local user. – Jacobian Feb 01 '18 at 17:18
  • 1
    You have to restart the docker daemon, otherwise it won't let members of the docker group to control the docker daemon – Murmel Feb 01 '18 at 17:20
  • But please keep in mind, that you are basically giving your $USER root privileges, see: [Manage Docker as a non-root user](https://docs.docker.com/install/linux/linux-postinstall/#manage-docker-as-a-non-root-user), – Murmel Feb 01 '18 at 17:22
  • I've just tried `sudo service docker stop` and `sudo service docker start`, but it did not help. – Jacobian Feb 01 '18 at 17:24
  • I've seen that reference and did exaсtly what they advise - `sudo usermod -aG docker $USER`. – Jacobian Feb 01 '18 at 17:25
  • But it did not help. – Jacobian Feb 01 '18 at 17:36
  • 3
    After changing users/groups you have to relogin, not just open new terminal. – Sergius Feb 01 '18 at 17:43
  • To add to the fun, I see that cached ssh sessions (from Mac to Ubuntu in my case) don't pick up the new group memberships created during the lifetime of the parent session. So, "logging in again" doesn't pick up the new groups. `newgrp docker` of course does put one into the group, as does starting a new (parent) ssh session, for example by ssh'ing into the host's IP address rather than its symbolic name. Docker's default error message (in some builds) about `http+docker://localunixsocket` does not help. – Andrew Beals Aug 04 '20 at 22:35

12 Answers12

281

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.

BMitch
  • 231,797
  • 42
  • 475
  • 450
  • 1
    This did not work for me, but I was using namespaces. I had to use ```--userns=host```. – Mr00Anderson Feb 12 '19 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. – Bruno Laturner Feb 26 '19 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 Feb 26 '19 at 21:02
  • 3
    @BMitch are you in NSA spying me? That is my exact config and bug. Thanks! Solved following https://askubuntu.com/q/1057258/259660 – Bruno Laturner Feb 27 '19 at 15:14
  • Related Red Hat bug for RHEL7 and Fedora 30: https://bugzilla.redhat.com/show_bug.cgi?id=1214104 – Jeremy Sep 08 '19 at 14:15
  • 4
    running `newgrp docker ` command is necessary to activate the changes to groups – Jay Modi Dec 06 '19 at 17:38
  • It seems almost all answers are variations of @BMitch's standard one :) but FWIW−because of Docker's daemon attack surface−, one may want to try the alias-based solution I propose [in my answer](https://stackoverflow.com/a/65956808/9164010): it addresses the same goal (being able to just write `docker run …` in one's terminal) but, to some extent, it would be a safer solution for a personal workstation. – ErikMD Jan 03 '22 at 20:55
80

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.

Nitish
  • 968
  • 6
  • 9
  • 7
    You're probably missing out on votes because people log out then forget to come back and upvote lols. – John Mee Feb 07 '20 at 07:51
  • 2
    I bet they're missing the upvotes cause the "Quick Fix" is a security disaster... The docker socket should never be accessible to world... – Aayla Secura Apr 17 '20 at 04: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 Apr 18 '20 at 05: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 Aug 09 '20 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. – Zura Sekhniashvili Aug 18 '22 at 06:53
43
  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
    
Pang
  • 9,564
  • 146
  • 81
  • 122
1nternetz
  • 559
  • 4
  • 7
  • 7
    Restarting the Docker daemon was a big one. Always forget to do that after adding user to Docker group :\ – Parth Patel Jul 29 '19 at 23:45
  • 1
    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 Jan 29 '21 at 18:13
  • 1
    A Docker service restart solved the issue after adding the group to the OS environment. Thank you! – Artfaith Sep 08 '22 at 05:55
  • I had to restart the daemon after creating the docker group. Upvoting this answer. – Ruzihm Dec 09 '22 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... – ElectRocnic Mar 22 '23 at 19:22
11

enter the command and explore docker without sudo command

sudo chmod 666 /var/run/docker.sock
Sergei Basharov
  • 51,276
  • 73
  • 200
  • 335
ashique
  • 935
  • 2
  • 8
  • 26
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
Tarun Lalwani
  • 142,312
  • 9
  • 204
  • 265
3

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

Curious
  • 197
  • 1
  • 7
1

***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
Adam Winter
  • 1,680
  • 1
  • 12
  • 26
0

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.

GSAN
  • 648
  • 6
  • 29
0

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
    
ErikMD
  • 13,377
  • 3
  • 35
  • 71
  • @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 Jan 03 '22 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](https://stackoverflow.com/a/48569858/9164010), 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 Jan 03 '22 at 20:44
0

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

Senthuran
  • 1,583
  • 2
  • 15
  • 19
-2

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

Tom Carrick
  • 6,349
  • 13
  • 54
  • 78
  • 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 Jan 03 '22 at 21:10
-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

solxget
  • 1
  • 1