【Docker】alpine containerでless: unrecognized option: Xが発生するとき

どうもてぃ。
railsを僕の大好きなdockerで動かしていて、今回も素敵なエラーが発生したので対処法をここに記す。
発生したエラー内容
% docker compose run --rm web rails c
[+] Running 2/0
⠿ Container postgres Running 0.0s
⠿ Container redis Running 0.0s
ruby/3.0.0 isn't supported by this pry-doc version
Running via Spring preloader in process 29
Loading development environment (Rails 6.1.1)
[1] pry(main)> Company.find(62).products
Company Load (0.7ms) SELECT "companies".* FROM "companies" WHERE "companies"."id" = $1 LIMIT $2 [["id", 62], ["LIMIT", 1]]
Product Load (0.8ms) SELECT "products".* FROM "products" WHERE "products"."company_id" = $1 [["company_id", 62]]
less: unrecognized option: X
BusyBox v1.32.1 () multi-call binary.
Usage: less [-EFIMmNSRh~] [FILE]...
View FILE (or stdin) one screenful at a time
-E Quit once the end of a file is reached
-F Quit if entire file fits on first screen
-I Ignore case in all searches
-M,-m Display status line with line numbers
and percentage through the file
-N Prefix line number to each line
-S Truncate long lines
-R Remove color escape codes in input
-~ Suppress ~s displayed past EOF
デフォルトのalpine linuxにはlessコマンドは入ってないってことらしい。alpine imageが最小構成だから当たり前か。
解決方法
二点あります。
- Dockerfileのランタイムパッケージにlessを追加する
- apk add lessで追加する
alpine imageを利用しているのに、サーバー起動に不要なパッケージを入れるのは好ましくないので、ランタイムパッケージにlessを追加するのは却下。
apk add lessを立ち上げてるコンテナに直接叩きこむ方法でいきましょう。
# コンテナでコマンドを叩かずシェルを起動する(bashでもおk) $ docker compose run --rm web sh /myapp # apk add less . . . /myapp # rails c [1] pry(main)> Company.first.products # => 無事表示された
終わり
この記事を見たものに幸あれ。