LoginSignup
31
11

More than 1 year has passed since last update.

Docker Desktop for Apple siliconでseleniumを使う

Last updated at Posted at 2022-04-27

こんにちは。
m1 macを使ってdocker環境でweb開発をしている際に、統合テストを行うためにselenium/standalone-chrome-debugを使おうとしたらエラーが出たので備忘録として修正の流れを残しておこうと思います。

エラー解消までの流れ

自分が直面したエラー

Docker Desktop for Apple siliconでselenium/standalone-chrome-debugを使おうとしたらエラーが出力されました。
厳密にいえば、コンテナ自体は立ち上がるのですが、seleniumを使用してtestプログラムを動作させようとすると下記のエラーが出力されます。

Selenium::WebDriver::Error::UnknownError:
     unknown error: Chrome failed to start: crashed.
     (chrome not reachable)
     (The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)

docker-seleniumのgithubを読んでみる

docker-seleniumのgithubをみると、こんな文面がありました。

For experimental docker container images, which run on platforms such as the Mac M1 or Raspberry Pi, see the community driven repository hosted at seleniumhq-community/docker-seleniarm. These images are built for three separate architectures: linux/arm64 (aarch64), linux/arm/v7 (armhf), and linux/amd64.

ざっくり翻訳すると

  • どうやら普通?のdocker-seleniumはm1 macで動かないらし
  • linux/arm64 (aarch64), linux/arm/v7 (armhf), linux/amd64 用のimageがseleniumhq-community/docker-seleniarm にあるらしい
  • これは実験的なdocker container imageらしい

解決方法

ということで、言われるままに実験的なdocker container imageであるseleniarm/standalone-chromium:latestを使ってみました。
chromeではなくChromiumとなっていますが、この実験的なdocker container imageはchromeの提供がされていないので仕方ないっぽいです。

結果としては問題なくtestプログラムが正常に動作しました!

色々調べてできた疑問点

エラーの解消はできたものの、なんでDocker Desktop for Apple siliconだとchromeが動かないんだ? という疑問が出てきたので、ちょっと調べてみました。
自分の解釈に自信がないので、きちんと知りたいかたは紹介しているリンク先を読んでください。

issueを読んでみる

実験的なdocker container imageの詳細としてseleniumのgithubで紹介されているissueを読んでみました。
そこで、こんな投稿がありました。

There are no Google Chrome binaries, only Chromium, for ARM64, and the Selenium maintainers prefer to support the browsers used most by users, which is Google Chrome.

ARM64用のGoogle Chromeのバイナリはないらしいです。
いまいち疑問が解消されないので、もうちょい調べます。

m1 mac用のdockerでchromeが動かない原因を調べてみた

めちゃくちゃわかりやすい記事がありました。

Docker Desktop for Apple silicon にはQEMUのバイナリエミュレーションによる限界として inotify 関連のAPIが動かないと書かれていますが、Chromeは inotify を利用しているせいか、起動するとQEMUがcore dumpします。

ということは

  • Docker dektop for apple siliconではバイナリエミュレータとしてQEMUを使っている
  • QEMUではinotify関連のapiが動かない
  • chromeはinotifyを使っている
  • つまりm1 macで、docker desktopを使うときはchromeは動かない
  • chrominiumを使うしかない...

ということかなと思います。
疑問は解消されたのですが、念の為もうちょい調べます。

Docker Desktop for Apple siliconを読んでみた

上記の記事で紹介されていた、Docker Desktop for Apple siliconを読んでみます。

However, attempts to run Intel-based containers on Apple silicon machines under emulation can crash as qemu sometimes fails to run the container. In addition, filesystem change notification APIs (inotify) do not work under qemu emulation. Even when the containers do run correctly under emulation, they will be slower and use more memory than the native equivalent.

  • ファイルシステム変更通知API(inotify)はqemuのエミュレーション下では動作しないらしいです。
  • エミュレーション下でコンテナが正しく実行された場合でも、ネイティブの同等品よりも速度が低下し、より多くのメモリを使用することになるそうです。

なんとなく疑問が解決されたし、新たな知識もついたので一旦ここで調べ物は止めておこうと思います。
ただ、新たな疑問点として

  • なんでDocker Desktop for Apple siliconではqemu使ってるんだろうか?
  • なんでqemuではinotifyが動かないんだろうか?

というものが出てきました。

今までは低レイヤーに対してあまり興味はなかったのですが、今回調べてみてめちゃくちゃ面白かったです。
上記の疑問も解消できるように調べてこうと思います。

31
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
31
11