1. 程式人生 > >”Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker

”Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker

轉載地址:Docker啟動Get Permission Denied

以下問題及解決方法都在Ubuntu16.04下,其他環境類似

問題描述

安裝完docker後,執行docker相關命令,出現

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

原因

摘自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命令

解決方法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正常使用