Docker启动时提示Get-Permission-Denied-while-trying-to-connect解决方法

7次阅读

共计 1208 个字符,预计需要花费 4 分钟才能阅读完成。

环境描述

vmware15 虚拟机安装 centos7.4 64 位系统,docker 版本 19.03.2

问题描述

安装完 docker 后,执行 docker 相关命令

docker run ubuntu:15.10 /bin/echo "Hello world"

出现如下提示:

docker: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post http://%2Fvar%2Frun%2Fdocker….: 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 用户,需要 root 权限才能访问。

解决方法 1

使用 sudo 获取管理员权限,运行 docker 命令

# 如下命令可以正常执行
sudo docker run ubuntu:15.10 /bin/echo "Hello world"

解决方法 2

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 正常使用

本文转自:https://www.fengjunzi.com/blog-25467.html

欢迎访问我的网站:风君子博客,微信号 fj3702 交流

正文完
 0