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交流