问题描述

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

“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”

image.png

原因分析

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

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权限 才能访问。

解决方法

  1. #添加docker用户组
  2. $ sudo groupadd docker
  3. #检测当前用户是否已经在docker用户组中,其中XXX为用户名,例如我的,liangll
  4. $ sudo gpasswd -a <当前用户名> docker
  5. #将当前用户添加至docker用户组
  6. $ sudo gpasswd -a <当前用户名> docker
  7. #更新docker用户组
  8. $ newgrp docker

image.png

检查是否更新成功

再次执行”docker version”命令,发现不再出现”Got permission denied”权限报错
image.png

重启系统

重开一个终端还是报同样的错,重启系统后解决

参考:https://blog.csdn.net/liangllhahaha/article/details/92077065