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

环境描述

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

问题描述

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

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

出现以下提示:ubuntu

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上的一段话centos

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.socket

大概的意思就是: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相关命令centos7

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

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

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