Ubuntu18.04安装docker,执行docker命令时提示权限限制:“Got permission denied while trying to …”--非root模式执行docker命令

一、问题:

在我的Ubuntu18.04安装好docker,执行如“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.26/images/json: dial unix /var/run/docker.sock: connect: permission denied“

二、原因:

查找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用户,其他用户只能使用sudo指令才能使用Unix socket,也即docker守护进程需以root用户执行、或其他用户用sudo命令执行……

三、解决办法:

办法一:使用sudo指令、或切换到root用户模式;

办法二:如上述英文最后一段所述,创建用户组docker、并将当前用户名添加到该组中即可,因为docker守护进程启时会赋予以docker命名的用户组对Unix socket “可读可写”权限,即用户组中成员可正常使用docker相关操作、指令……如下:

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

注意:其中$USER为当前用户名,如我的ubuntu18.04用户名为:zz414,将上述指令中$USER改为zz414即可

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值