一、概述
普通用戶執行命令:docker ps報錯,具體信息如下:
docker: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post http://%2Fvar%2Frun%2Fdocker.sock/v1.38/containers/create: dial unix /var/run/docker.sock: connect: permission denied.
出現上面問題是因為:
Manage Docker as a non-root user
即:管理Docker的不是root用戶
原文表述:
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權限才能訪問。
二、解決方案
方案一:使用sudo獲取管理員權限,運行docker命令
方案二:添加docker group組,將用戶添加進去
環境說明
操作系統:centos 7.6
docker版本:19.03.5
由於采用的是jumpserver方式登錄,開發人員使用的是develop賬號。
docker安裝之后,默認會創建組docker,因此不需要再創建了。
執行命令
使用root賬號登錄目標服務器
#將登陸用戶develop加入到docker用戶組中 gpasswd -a develop docker #更新用戶組 newgrp docker
切換到develop用戶進行測試
su develop
docker ps
輸出正常,則表示成功了。
本文參考鏈接:
https://www.jianshu.com/p/a0cf03605c42