Linux執(zhí)行Docker命令提示Get Permission Denied的解決方案
一、問(wèn)題描述
安裝完docker后,執(zhí)行docker相關(guān)命令,出現(xiàn)以下報(bào)錯(cuò):
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 上的一段話,對(duì)此異常的說(shuō)明如下:
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 進(jìn)程使用 Unix Socket 而不是 TCP 端口。
- 而默認(rèn)情況下,Unix socket 屬于 root 用戶,需要 root 權(quán)限才能訪問(wèn)。
三、解決方法1
使用 sudo 獲取管理員權(quán)限,運(yùn)行 docker 命令時(shí)在前面加上 sudo
四、解決方法2
docker 守護(hù)進(jìn)程啟動(dòng)的時(shí)候,會(huì)默認(rèn)賦予名字為 docker 的用戶組讀寫 Unix socket 的權(quán)限,因此只要?jiǎng)?chuàng)建 docker 用戶組,并將當(dāng)前用戶加入到 docker 用戶組中,那么當(dāng)前用戶就有權(quán)限訪問(wèn) Unix socket 了,進(jìn)而也就可以執(zhí)行 docker 相關(guān)命令了。
具體操作命令如下:
sudo groupadd docker # 添加docker用戶組 sudo gpasswd -a $USER docker # 將登陸用戶加入到docker用戶組中 newgrp docker # 更新用戶組 docker images # 測(cè)試docker命令在不加sudo時(shí)是否可以使用
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
docker容器互聯(lián)的實(shí)現(xiàn)步驟
容器互聯(lián)是除了端口映射外另一種可以與容器通信的方式,本文就來(lái)介紹一下docker容器互聯(lián)的實(shí)現(xiàn)步驟,感興趣的可以了解一下2023-12-12教你使用docker安裝elasticsearch和head插件的方法
這篇文章主要介紹了docker安裝elasticsearch和head插件,安裝時(shí)需要下載鏡像和修改系統(tǒng)參數(shù),本文分流程給大家講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04docker.service啟動(dòng)報(bào)錯(cuò)的一次排查詳解
這篇文章主要介紹了docker.service啟動(dòng)報(bào)錯(cuò)的一次排查詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11docker中nginx卸載、安裝、配置及掛載詳細(xì)教程
這篇文章主要給大家介紹了關(guān)于docker中nginx卸載、安裝、配置及掛載的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹是非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-09-09