0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Docker Compose で `php artisan serve` したコンテナの停止に10秒かかる問題の解決

Posted at

問題

以下のようなDockerfileで起動しているような開発用のLaravelコンテナが、 restartや再build後のup等で必ず10秒かかる。

Dockerfile
FROM php8.2-fpm-alpine
...
CMD ["php", "artisan", "serve", "--host", "0.0.0.0"]

原因

Docker Composeのよくある質問と回答 - サービスの再作成や停止に10秒かかるのはどうして?に原因は記載されています。

しかし、ここに書いてあるように ["php", ...] 形式で書いても、私の場合解決しませんでした。

根本的な問題としてはdocker composeのstopのシグナルが SIGTERM であり、 php artisan serve は SIGINT を期待していることにあります。

解決策

簡単なラッパスクリプトを用意してSIGTERMをSIGINTに変換することで即座に停止できるようになりました。
(1 2 3 15は拾うシグナルでもうちょいちゃんと狭められます...)

artisan-serve-wrapper.sh
php artisan serve --host 0.0.0.0 &
pid=$!
trap "kill -SIGINT $pid" 1 2 3 15
wait $pid
FROM php8.2-fpm-alpine
...
CMD ["./artisan-serve-wrapper.sh"]

参考文献

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?