dockerコンテナ(ubuntu 20.04 LTS arm64)内でChromiumを動かすのが大変だった件
経緯
ubuntu 18.04 LTSのサポートが1年を切ったのでubuntu 20.04 LTSにアップデートしようとしていた。
実サーバー側の検証はそれ程大きな問題はなかったがCI環境でE2Eテストを行う部分で、コンテナ内でChromium headlessを使っており、これが思いの外難産だった。
技術的背景
1. Google Chromeが使えない
本来、googleがバイナリ配布しているChromeが使えれば良いのだが、Linux arm64バイナリは提供されていない。 なのでChromiumを使う必要がある
2. Ubuntu 19.10以降、ChromiumのAPT(deb)パッケージが廃止
バイナリはsnap版に移行し、debパッケージはsnap installに迂回するようになった。
3. Dockerコンテナ内でsnapが使えない
普通にDocker buildするとsnapdが立てられないのでsnap install が失敗する。
# snap install chromium error: cannot communicate with server: Post http://localhost/v2/snaps/chromium: dial unix /run/snapd.socket: connect: no such file or directory
解決法
Dockerコンテナでsnapdを動かす
偉い人が作ったDockerコンテナジェネレータがあったので、これを改造して使ってみた。
これを紐解くと以下のようなDockerfileになる。
なるほど、、systemd経由でsnapdを起動する構造はそのままで、/sbin/init
から上げて行くわけね。
Dockerfile
FROM --platform=linux/arm64 ubuntu:20.04 ENV container docker ENV PATH "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin" ENV LANG C.UTF-8 ENV LC_ALL C.UTF-8 RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y fuse snapd snap-confine squashfuse sudo init && apt-get clean && dpkg-divert --local --rename --add /sbin/udevadm && ln -s /bin/true /sbin/udevadm RUN systemctl enable snapd VOLUME ["/sys/fs/cgroup"] STOPSIGNAL SIGRTMIN+3 CMD ["/sbin/init"]
ハマった点
無事snapdは上がり、snapコマンドは通る
snap list Name Version Rev Tracking Publisher Notes core 16-2.56.2+git3949.06393d8a6 13434 latest/edge canonical** core
ところが、実際のinstallは失敗する
# snap install chromium error: cannot perform the following tasks: - Run configure hook of "chromium" snap if present (run hook "configure": aa_is_enabled() failed unexpectedly (No such file or directory): No such file or directory)
調べていくと、workaroundを提示している人が居た
# mount -t securityfs securityfs /sys/kernel/security
確かに、/sys/kernel/security
が空だったのでmountすることで見事に解決した。
# snap install chromium chromium 102.0.5005.115 from Canonical✓ installed
しかしDockerfileで解決出来ないのは困ったな。。