【亲测有效】运行docker ps 出现Got permission denied问题的解决方案 - Go语言中文社区

【亲测有效】运行docker ps 出现Got permission denied问题的解决方案


问题描述

今天在运行 docker ps 命令的时候出现如下问题:

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.38/containers/json: dial unix /var/run/docker.sock: connect: permission denied

1100338-20181101162225835-101447073.png

看提示信息好像是权限不够,我们试试 root 权限:

1100338-20181101162225491-20134882.png

我们发现用 root 权限就可以使用 docker 相关命令,那我们想在普通用户下使用 docker 相关命令,这该怎么办呢?

我们去 Docker Mannual 找到原因,原因如下:

Manage Docker as a non-root user

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 use sudo when you use the docker command, create a Unix group called docker and add users to it. When the docker daemon starts, it makes the ownership of the Unix socket read/writable by the docker group.

上面这段话的意思是 docker 进程使用 Unix Socket 而不是 TCP 端口。而默认情况下, Unix socket 属于 root 用户,需要 root 权限才能访问。

我们该怎么解决这个问题呢?

解决方案一

使用 sudo 获取管理员权限,运行 docker 命令(当然我不推荐这种方法,因为我没成功过,似乎还是出现了如上问题)

解决方案二

由于 docker 守护进程启动的时候,会默认赋予名字为 docker 的用户组读写 Unix socket 的权限,因此只要创建 docker 用户组,并将当前用户加入到 docker 用户组中,那么当前用户就有权限访问 Unix socket 了,进而也就可以执行 docker 相关命令了。

我们可以使用如下命令解决问题:

sudo groupadd docker     #添加docker用户组
sudo gpasswd -a $USER docker     #将登陆用户加入到docker用户组中
newgrp docker     #更新用户组
docker ps    #测试docker命令是否可以使用sudo正常使用

然后我们可以完美地解决了这个问题,效果如下:

1100338-20181101162224934-1651950297.png

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/weixin_33834137/article/details/86134265
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-04-19 10:28:37
  • 阅读 ( 1154 )
  • 分类:Go

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢