zaki work log

作業ログやら生活ログやらなんやら

Debian11(Bullseye)でRootless Dockerインストールしてコンテナをデプロイ

Debian10では依存パッケージのバージョンが古くてインストールできなかったルートレスDocker、先々週リリースされたDebian11ではパッケージバージョンが新しくなって全て要件を満たしていたので試してみた。

docs.docker.com

Deiban10だとうまくいかないパターンは以下

zaki-hmkc.hatenablog.com

環境

root@debian11:~# cat /etc/debian_version 
11.0

ルートレスDockerのインストール自体は公式ドキュメントの通りセットアップすれば基本的に動く。
以下は最小構成でOSインストールした直後の状態(通常のDockerが未インストール)の場合。

ちなみに、sudoまだ入ってないので、rootにsuして実行してる。

準備

uidmap

root@debian11:~# apt-get install uidmap
zaki@debian11:~$ id -u
1000
zaki@debian11:~$ whoami 
zaki
zaki@debian11:~$ grep ^$(whoami): /etc/subuid
zaki:100000:65536
zaki@debian11:~$ grep ^$(whoami): /etc/subgid
zaki:100000:65536

dbus-user-session

root@debian11:~# apt-get install dbus-user-session

このあと一度ログインしなおす。

overlay2

一旦省略

slirp4netns

root@debian11:~# apt search slirp4netns 
ソート中... 完了
全文検索... 完了  
slirp4netns/stable 1.0.1-2 amd64
  User-mode networking for unprivileged network namespaces

うむ、バージョン要件を満たしている。

root@debian11:~# apt-get install slirp4netns
:
:
root@debian11:~# slirp4netns --version
slirp4netns version 1.0.1
commit: 6a7b16babc95b6a3056b33fb45b74a6f62262dd4
libslirp: 4.4.0

install

docs.docker.com

install docker

まずは普通に

root@debian11:~# apt-get update
root@debian11:~# apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg \
    lsb-release
root@debian11:~# curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
root@debian11:~# echo \
  "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian \
  $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null

インストールする。

root@debian11:~# apt-get update
root@debian11:~# apt-get install docker-ce docker-ce-cli containerd.io

この通り。

root@debian11:~# docker --version
Docker version 20.10.8, build 3967b7d

インストール完了するとサービスが有効になってるので止める。

root@debian11:~# systemctl disable --now docker.service docker.socket
Synchronizing state of docker.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install disable docker
Removed /etc/systemd/system/sockets.target.wants/docker.socket.
Removed /etc/systemd/system/multi-user.target.wants/docker.service.

rootless

一般ユーザーで以下実行。

zaki@debian11:~$ dockerd-rootless-setuptool.sh install
[INFO] Creating /home/zaki/.config/systemd/user/docker.service
[INFO] starting systemd service docker.service
+ systemctl --user start docker.service
+ sleep 3
+ systemctl --user --no-pager --full status docker.service
● docker.service - Docker Application Container Engine (Rootless)
     Loaded: loaded (/home/zaki/.config/systemd/user/docker.service; disabled; vendor preset: enabled)
     Active: active (running) since Sat 2021-08-28 18:03:56 JST; 3s ago
       Docs: https://docs.docker.com/go/rootless/
   Main PID: 2277 (rootlesskit)
      Tasks: 33
     Memory: 72.7M
        CPU: 255ms
     CGroup: /user.slice/user-1000.slice/user@1000.service/app.slice/docker.service
             ├─2277 rootlesskit --net=slirp4netns --mtu=65520 --slirp4netns-sandbox=auto --slirp4netns-seccomp=auto --disable-host-loopback --port-driver=builtin --copy-up=/etc --copy-up=/run --propagation=rslave /usr/bin/dockerd-rootless.sh
             ├─2287 /proc/self/exe --net=slirp4netns --mtu=65520 --slirp4netns-sandbox=auto --slirp4netns-seccomp=auto --disable-host-loopback --port-driver=builtin --copy-up=/etc --copy-up=/run --propagation=rslave /usr/bin/dockerd-rootless.sh
             ├─2305 slirp4netns --mtu 65520 -r 3 --disable-host-loopback --enable-sandbox --enable-seccomp 2287 tap0
             ├─2312 dockerd
             └─2328 containerd --config /run/user/1000/docker/containerd/containerd.toml --log-level info

 8月 28 18:03:56 debian11 dockerd-rootless.sh[2312]: time="2021-08-28T18:03:56.970908740+09:00" level=error msg="failed to mount overlay: operation not permitted" storage-driver=overlay
 8月 28 18:03:56 debian11 dockerd-rootless.sh[2312]: time="2021-08-28T18:03:56.983809483+09:00" level=warning msg="Unable to find cpu controller"
 8月 28 18:03:56 debian11 dockerd-rootless.sh[2312]: time="2021-08-28T18:03:56.983830087+09:00" level=warning msg="Unable to find io controller"
 8月 28 18:03:56 debian11 dockerd-rootless.sh[2312]: time="2021-08-28T18:03:56.983835839+09:00" level=warning msg="Unable to find cpuset controller"
 8月 28 18:03:56 debian11 dockerd-rootless.sh[2312]: time="2021-08-28T18:03:56.983989597+09:00" level=info msg="Loading containers: start."
 8月 28 18:03:57 debian11 dockerd-rootless.sh[2312]: time="2021-08-28T18:03:57.023743296+09:00" level=info msg="Default bridge (docker0) is assigned with an IP address 172.17.0.0/16. Daemon option --bip can be used to set a preferred IP address"
 8月 28 18:03:57 debian11 dockerd-rootless.sh[2312]: time="2021-08-28T18:03:57.060855981+09:00" level=info msg="Loading containers: done."
 8月 28 18:03:57 debian11 dockerd-rootless.sh[2312]: time="2021-08-28T18:03:57.068177847+09:00" level=info msg="Docker daemon" commit=75249d8 graphdriver(s)=vfs version=20.10.8
 8月 28 18:03:57 debian11 dockerd-rootless.sh[2312]: time="2021-08-28T18:03:57.068280606+09:00" level=info msg="Daemon has completed initialization"
 8月 28 18:03:57 debian11 dockerd-rootless.sh[2312]: time="2021-08-28T18:03:57.088288454+09:00" level=info msg="API listen on /run/user/1000/docker.sock"
+ DOCKER_HOST=unix:///run/user/1000/docker.sock /usr/bin/docker version
Client: Docker Engine - Community
 Version:           20.10.8
 API version:       1.41
 Go version:        go1.16.6
 Git commit:        3967b7d
 Built:             Fri Jul 30 19:54:22 2021
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.8
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.16.6
  Git commit:       75249d8
  Built:            Fri Jul 30 19:52:31 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.9
  GitCommit:        e25210fe30a0a703442421b0f60afac609f950a3
 runc:
  Version:          1.0.1
  GitCommit:        v1.0.1-0-g4144b63
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0
+ systemctl --user enable docker.service
Created symlink /home/zaki/.config/systemd/user/default.target.wants/docker.service → /home/zaki/.config/systemd/user/docker.service.
[INFO] Installed docker.service successfully.
[INFO] To control docker.service, run: `systemctl --user (start|stop|restart) docker.service`
[INFO] To run docker.service on system startup, run: `sudo loginctl enable-linger zaki`

[INFO] Creating CLI context "rootless"
Successfully created context "rootless"

[INFO] Make sure the following environment variables are set (or add them to ~/.bashrc):

export PATH=/usr/bin:$PATH
export DOCKER_HOST=unix:///run/user/1000/docker.sock

環境変数を追加する。PATHはすでにあるのでDOCKER_HOSTのみ。

zaki@debian11:~$ export DOCKER_HOST=unix:///run/user/1000/docker.sock
zaki@debian11:~$ docker image ls
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE
zaki@debian11:~$ docker container ls
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

docker runお試し

zaki@debian11:~$ docker run --rm -d -p 8080:80 nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
e1acddbe380c: Pull complete 
e21006f71c6f: Pull complete 
f3341cc17e58: Pull complete 
2a53fa598ee2: Pull complete 
12455f71a9b5: Pull complete 
b86f2ba62d17: Pull complete 
Digest: sha256:4d4d96ac750af48c6a551d757c1cbfc071692309b491b70b2b8976e102dd3fef
Status: Downloaded newer image for nginx:latest
76b82bf2fcc9d16e42142221bf52581ccb69a077e87b4863bff4913040d69e95
docker: Error response from daemon: OCI runtime create failed: container_linux.go:380: starting container process caused: process_linux.go:385: applying cgroup configuration for process caused: error while starting unit "docker-76b82bf2fcc9d16e42142221bf52581ccb69a077e87b4863bff4913040d69e95.scope" with properties [{Name:Description Value:"libcontainer container 76b82bf2fcc9d16e42142221bf52581ccb69a077e87b4863bff4913040d69e95"} {Name:Slice Value:"user.slice"} {Name:PIDs Value:@au [2645]} {Name:Delegate Value:true} {Name:MemoryAccounting Value:true} {Name:CPUAccounting Value:true} {Name:IOAccounting Value:true} {Name:TasksAccounting Value:true} {Name:DefaultDependencies Value:false}]: read unix @->/run/systemd/private: read: connection reset by peer: unknown.

あ、やっぱこのエラーでるな。。。

対応はdocker run errorsを確認。

zaki@debian11:~$ systemctl --user is-active dbus
inactive
zaki@debian11:~$ systemctl --user status dbus
● dbus.service - D-Bus User Message Bus
     Loaded: loaded (/usr/lib/systemd/user/dbus.service; static)
     Active: inactive (dead)
TriggeredBy: ● dbus.socket
       Docs: man:dbus-daemon(1)

dbus、インストールはされてるけど動いてないので有効化する。

zaki@debian11:~$ systemctl --user enable --now dbus
The unit files have no installation config (WantedBy=, RequiredBy=, Also=,
Alias= settings in the [Install] section, and DefaultInstance= for template
units). This means they are not meant to be enabled using systemctl.
 
Possible reasons for having this kind of units are:
• A unit may be statically enabled by being symlinked from another unit's
  .wants/ or .requires/ directory.
• A unit's purpose may be to act as a helper for some other unit which has
  a requirement dependency on it.
• A unit may be started when needed via activation (socket, path, timer,
  D-Bus, udev, scripted systemctl call, ...).
• In case of template units, the unit is meant to be enabled with some
  instance name specified.
zaki@debian11:~$ 
zaki@debian11:~$ 
zaki@debian11:~$ systemctl --user status dbus
● dbus.service - D-Bus User Message Bus
     Loaded: loaded (/usr/lib/systemd/user/dbus.service; static)
     Active: active (running) since Sat 2021-08-28 18:07:30 JST; 37s ago
TriggeredBy: ● dbus.socket
       Docs: man:dbus-daemon(1)
   Main PID: 2674 (dbus-daemon)
      Tasks: 1 (limit: 4675)
     Memory: 496.0K
        CPU: 2ms
     CGroup: /user.slice/user-1000.slice/user@1000.service/app.slice/dbus.service
             └─2674 /usr/bin/dbus-daemon --session --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only

 8月 28 18:07:30 debian11 systemd[584]: Started D-Bus User Message Bus.

コンテナを再実行する。

zaki@debian11:~$ docker run --rm -d -p 8080:80 nginx
1872f49375bfd1e86839be7903c0ffb11ac97772926854b50782438c27a8a87f
zaki@debian11:~$ curl localhost:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>

[snip]

動いた。


おまけ

Debian11入れて割とすぐくらいのタイミングで試してもうまくいかず、藁にもすがる思いでツイートしたら助けてもらいました!

というのも実は私が試したときはdbus関連のパッケージについて記載がドキュメントに載ってなくて、このやりとりのあとに、ドキュメント修正のPR出してしてくれたらしい…感謝です。

github.com

エラーメッセージもわかりやすくなるかも。

github.com

コミュニティの開発、すごい。。


ちなみにディストリビューションでいうと、「Distribution-specific hint」のところに

Note: We recommend that you use the Ubuntu kernel.

と書いてある笑