解决`Got permission denied while trying to connect to the Docker daemon socket`

886 阅读1分钟

事情的起因是我在 Windows 10 下套娃:

在 Windows 10 的 WSL2 功能下安装了 Ubuntu 20.04 LTS,然后在 Ubuntu 20.04 LTS 中安装了 Docker,又在 Docker 下安装了 Ubuntu 20.04……(主要是为了练习 Linux 相关命令

正常的安装了 Docker:

正常的执行 docker version 虽然在这儿有个 Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?,但感觉跟我后面的遇到的问题不太关联的亚子🤔

问题:Got permission denied while trying to connect to the Docker daemon socket

然后我就在执行 docker images 时,遭遇了错误:

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

解决方案

Docker Official Document - linux postinstall

翻了下官方文档得到如下的解决方案

  1. 创建 docker 用户组:sudo grounpadd docker

    • 如果之前不存在 docker 用户组,则会被创建
    • 若是存在,则会提示已存在。
  2. 检测当前用户,是否存在于 docker 用户组:sudo gpasswd -a $username docker

    • 其中 username,是指你系统当前登录的用户名
      • 若是不清楚,可通过 whoami 命令查询

    • 若是你写的 username 存在,但不是当前系统登录用户,则也会被添加。
    • 若是 username 根本就不存在,则会被提示不存在。
  3. 若是当前系统登录的用户,就是你想要添加到 docker 用户组的用户,且未被添加进 docker 用户组,你可以直接这样写添加命令:sudo gpasswd -a $USER docker

  4. 激活刚刚对 docker 用户组的更改:newgrp docker

  5. 现在执行 docker images 就没问题了

于是,大功告成!