解決Ubuntu18.04啓動Docker"Got permission denied while trying to connect to the Docker daemon socket"問題

目錄docker

1 問題描述json

2 緣由分析socket

3 解決方法unix

4 檢查是否更新成功code


1 問題描述

在終端執行"docker version"命令,出現以下報錯:blog

」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「進程

2 緣由分析

來自docker mannual:ip

Manage Docker as a non-root userit

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

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權限才能訪問。

3 解決方法

sudo groupadd docker #添加docker用戶組
sudo gpasswd -a $XXX docker #檢測當前用戶是否已經在docker用戶組中,其中XXX爲用戶名,例如個人,liangll
sudo gpasswd -a $USER docker #將當前用戶添加至docker用戶組
newgrp docker #更新docker用戶組

4 檢查是否更新成功

再次執行"docker version"命令,發現再也不出現"Got permission denied"權限報錯