Linux 上一般不建议直接使用 root 用户,但普通用户使用 docker 时会提示权限不够。

复现问题

添加用户

sudo useradd -m -s /bin/bash k8scat

执行命令

docker ps

# 输出
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.40/containers/json: dial unix /var/run/docker.sock: connect: permission denied

解决问题

查看 /var/run/docker.sock 的权限

ls -la /var/run/ | grep docker.sock

# 输出
srw-rw----  1 root    docker     0 Dec 21 16:20 docker.sock

添加用户至 docker 用户组

sudo usermod -aG docker hsowan

登录 docker 用户组

newgrp docker

再次执行命令

docker version

# 输出
Client: Docker Engine - Community
 Version:           19.03.8
 API version:       1.40
 Go version:        go1.12.17
 Git commit:        afacb8b7f0
 Built:             Wed Mar 11 01:25:46 2020
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.8
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.12.17
  Git commit:       afacb8b7f0
  Built:            Wed Mar 11 01:24:19 2020
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.2.13
  GitCommit:        7ad184331fa3e55e52b890ea95e65ba581ae3429
 runc:
  Version:          1.0.0-rc10
  GitCommit:        dc9208a3303feef5b3839f4323d9beb36df0a9dd
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683