当我们用root身份在服务器部署docker后,如果想用普通用户操作docker应该如何做呢?
[root@shanghai-aliyun ~]
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
423435db3a3f nginx "/docker-entrypoint.…" 5 weeks ago Up 3 weeks 0.0.0.0:180->80/tcp nginx_nginx_1
9edbbba02a8b fluentd_fluentd "/bin/entrypoint.sh …" 5 weeks ago Up 5 weeks 5140/tcp, 0.0.0.0:24224->24224/tcp, 0.0.0.0:24224->24224/udp fluentd_fluentd_1
[root@shanghai-aliyun ~]
[root@shanghai-aliyun ~]
[root@shanghai-aliyun ~]
[root@shanghai-aliyun ~]
[cntsp@shanghai-aliyun ~]$ pwd
/home/cntsp
[cntsp@shanghai-aliyun ~]$ 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.39/containers/json: dial unix /var/run/docker.sock: connect: permission denied
[cntsp@shanghai-aliyun ~]$ sudo docker ps
We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:
[sudo] password for cntsp:
cntsp is not in the sudoers file. This incident will be reported.
分析/etc/sudoers文件
[root@shanghai-aliyun ~]
-r--r-----. 1 root root 4.3K Nov 28 2019 /etc/sudoers
[root@shanghai-aliyun ~]
Defaults !visiblepw
Defaults always_set_home
Defaults match_group_by_gid
Defaults always_query_group_plugin
Defaults env_reset
Defaults env_keep = "COLORS DISPLAY HOSTNAME HISTSIZE KDEDIR LS_COLORS"
Defaults env_keep += "MAIL PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS LC_CTYPE"
Defaults env_keep += "LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES"
Defaults env_keep += "LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE"
Defaults env_keep += "LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY"
Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin
root ALL=(ALL) ALL
%wheel ALL=(ALL) ALL
下面我们先把cntsp用户加入 wheel 组内,在执行 sudo docker ps 命令
[root@shanghai-aliyun ~]
uid=1003(cntsp) gid=1003(cntsp) groups=1003(cntsp)
[root@shanghai-aliyun ~]
Adding user cntsp to group wheel
[root@shanghai-aliyun ~]
[root@shanghai-aliyun ~]
[root@shanghai-aliyun ~]
uid=1003(cntsp) gid=1003(cntsp) groups=1003(cntsp),10(wheel)
[root@shanghai-aliyun ~]
[root@shanghai-aliyun ~]
[cntsp@shanghai-aliyun root]$
[cntsp@shanghai-aliyun root]$
[cntsp@shanghai-aliyun root]$ pwd
/root
[cntsp@shanghai-aliyun root]$ cd
[cntsp@shanghai-aliyun ~]$ pwd
/home/cntsp
[cntsp@shanghai-aliyun ~]$ sudo docker ps
[sudo] password for cntsp:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
423435db3a3f nginx "/docker-entrypoint.…" 5 weeks ago Up 3 weeks 0.0.0.0:180->80/tcp nginx_nginx_1
9edbbba02a8b fluentd_fluentd "/bin/entrypoint.sh …" 5 weeks ago Up 5 weeks 5140/tcp, 0.0.0.0:24224->24224/tcp, 0.0.0.0:24224->24224/udp fluentd_fluentd_1
[root@shanghai-aliyun ~]
[root@shanghai-aliyun ~]
[root@shanghai-aliyun ~]
[cntsp@shanghai-aliyun root]$ whoami
cntsp
[cntsp@shanghai-aliyun root]$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
423435db3a3f nginx "/docker-entrypoint.…" 5 weeks ago Up 3 weeks 0.0.0.0:180->80/tcp nginx_nginx_1
9edbbba02a8b fluentd_fluentd "/bin/entrypoint.sh …" 5 weeks ago Up 5 weeks 5140/tcp, 0.0.0.0:24224->24224/tcp, 0.0.0.0:24224->24224/udp fluentd_fluentd_1
[cntsp@shanghai-aliyun root]$ 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.39/containers/json: dial unix /var/run/docker.sock: connect: permission denied
不好意思,我特懒,我希望普通用户执行docker命令的时候,不要多敲这个sudo命令,想直接 docker ps 就给我正常返回,我们看到的是 /var/run/docker.sock 链接权限拒绝
[cntsp@shanghai-aliyun root]$ ls -lh /var/run/docker.sock
srw-rw---- 1 root docker 0 Jul 14 23:34 /var/run/docker.sock
[root@shanghai-aliyun ~]
dockerroot:x:993:
docker:x:992:
[root@shanghai-aliyun ~]
uid=1003(cntsp) gid=1003(cntsp) groups=1003(cntsp),10(wheel)
[root@shanghai-aliyun ~]
Adding user cntsp to group docker
[root@shanghai-aliyun ~]
uid=1003(cntsp) gid=1003(cntsp) groups=1003(cntsp),10(wheel),992(docker)
[root@shanghai-aliyun ~]
[cntsp@shanghai-aliyun root]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
423435db3a3f nginx "/docker-entrypoint.…" 5 weeks ago Up 3 weeks 0.0.0.0:180->80/tcp nginx_nginx_1
9edbbba02a8b fluentd_fluentd "/bin/entrypoint.sh …" 5 weeks ago Up 5 weeks 5140/tcp, 0.0.0.0:24224->24224/tcp, 0.0.0.0:24224->24224/udp fluentd_fluentd_1
`
|