mopoki’s blog

技術的なものの備忘録

root以外の一般ユーザーが、sudoを使わずにdockerをコントロールするにはどう設定すればいいか

やりたいこと

Docker daemon は通常 root ユーザ だけが操作できて、他のユーザーは sudo を使用しないとアクセスできない。

kaユーザーという、OSインストール時に作成した一般ユーザーで例示する
[ka@station ~]$ id
uid=1000(ka) gid=1000(ka) groups=1000(ka) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023

docker が起動しているか確認する
[ka@station ~]$ systemctl status docker
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
   Active: active (running) since 土 2020-04-18 00:49:04 JST; 29min ago
     Docs: https://docs.docker.com
 Main PID: 6379 (dockerd)
    Tasks: 8
   Memory: 148.6M
   CGroup: /system.slice/docker.service
           mq6379 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

docker.service が動いているので docker コマンドは権限さえあれば通るはず。
[ka@station ~]$ 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

このように権限取得が拒否されたとなり、操作ができない。

visudo により特定のユーザーが sudo をできるように設定すればよいが、常に sudo を打つのは面倒くさい。そのため、sudoを入力しなくてもよいように設定する。

確認環境

OS:CentOS 7

[ka@station ~]$ cat /etc/redhat-release
CentOS Linux release 7.7.1908 (Core)

docker: 19.03.8

[ka@station ~]$ docker --version
Docker version 19.03.8, build afacb8b

設定方法

実は Docker公式ドキュメントの Post-installation steps for Linux に方法が記載されている。

Post-installation steps for Linux | Docker Documentation

To create the docker group and add your user:
1. Create the docker group.
2. Add your user to the docker group.
3. Log out and log back in so that your group membership is re-evaluated.

docker グループに一般ユーザーを所属させればよい。

というわけで、公式ドキュメントに沿って設定してみる。

設定作業

まず group がどうなっているかを確認する。cat /etc/groupgetent group で表示する。

$ cat /etc/group
root:x:0:
・・・省略・・・
ka:x:1000:ka
docker:x:982:

Docker Engine をインストールする過程で docker グループ が作られていた。

docker グループに一般ユーザーを所属させる。

sudoの設定をしていない想定なので、rootユーザーになってから実施する
[ka@station ~]$ su -
[root@station ~]# usermod -aG docker ka
[root@station ~]# exit
[ka@station ~]$

結果を確認する
[ka@station ~]$ id ka
uid=1000(ka) gid=1000(ka) groups=1000(ka),982(docker)

usermod の -a は --appendのオプションでユーザーをサブグループに追加する。必ずサブグループを指定する -G オプションと一緒に使う。
※なぜか日本語の man には説明が無かった。

これで動くか確認する。

[ka@station ~]$ 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

あら、ダメだった。

ドキュメントを確認すると、 Post-installation steps for Linux | Docker Documentation

3. Log out and log back in so that your group membership is re-evaluated.
If testing on a virtual machine, it may be necessary to restart the virtual machine for changes to take effect.
On a desktop Linux environment such as X Windows, log out of your session completely and then log back in.
On Linux, you can also run the following command to activate the changes to groups: $ newgrp docker

一旦ログアウトするなどセッションをやり直さないといけないと書いてある。また、newgrp コマンドでグループの有効化をやればいけるとある。

というわけで、newgrp コマンドをやってみる。

[ka@station ~]$ newgrp docker
(特になにも出力なく終了)

グループ所属がどうなったかを確認する
[ka@station ~]$ id
uid=1000(ka) gid=982(docker) groups=982(docker),1000(ka) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023

主グループが gid=1000(ka) から gid=982(docker)に変わっている。

newgrpコマンドはログインセッション中の現在の主グループを指定したグループに変更するものなので、想定通りの動作になっている。

これで dockerコマンドが通るはずなのでやってみる。

[ka@station ~]$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
(何も起動していなので空行だが、列名の表示はでた)

[ka@station ~]$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              fce289e99eb9        15 months ago       1.84kB
(インストールや設定検証を行うためのコンテナイメージ hello-world が見えた)

これで、一般ユーザーで sudo を打たなくても docker コマンドが使えるようになった。