非Root用户访问docker命令出现“Got permission denied while trying to connect to the Docker daemon socket“的解决方法

问题描述

在终端执行”docker version”、”docker info”等命令,出现如下报错:

Server:
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.24/info": dial unix /var/run/docker.sock: connect: permission denied
errors pretty printing info

原因分析

The Docker daemon binds to a Unix socket instead of a TCP port. By default that Unix socket is owned by the user root and other users can only access it using sudo. The Docker daemon always runs as the root user.

If you don’t want to preface the docker command with sudo, create a Unix group called docker and add users to it. When the Docker daemon starts, it creates a Unix socket accessible by members of the docker group.

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

docker进程使用Unix Socket而不是TCP端口。而默认情况下,Unix socket属于root用户,因此需要root权限 才能访问。

解决方案

正确的是将当前用户加入docker组:

sudo gpasswd -a ${USER} docker

然后退出当前用户比如切换为root,再次切换为刚登录的用户。然后执行docker相关命令就ok了。


还有一种特殊情况,就是用户组的GID和Docker安装时的GID有出入,例如删除组以后新建等情况,docker.sock的文件权GID有变化就无法正常关联上docker用户组:

$ sudo ls -al /var/run/docker.sock 
srw-rw---- 1 root 998 0 Sep 20 03:09 /var/run/docker.sock

在这种情况下,您可以尝试更改 /var/run/docker.sock 文件的组所有权。

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

评论

  1. 灵绝
    Windows Chrome 106.0.0.0
    2 年前
    2022-10-17 22:49:37

    麦麦博主,我非常喜欢您的写作风格,来这里申请个友链
    网站名:明伐灵绝
    网址:https://forevers.love
    贵站链接已加!

    • 博主
      灵绝
      Windows Firefox 106.0
      2 年前
      2022-10-25 17:16:27

      感谢你的喜欢,已经添加贵站链接到本站友情链接中!

发送评论 编辑评论


				
上一篇
下一篇