92
82

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コンテナ、落ちるの遅すぎ…?

Last updated at Posted at 2023-02-28

はじめに

dockerの研修をやってた時に、「うーん、毎回docker-composeを使っててnodeコンテナだけ落ちるのに10秒以上かかるんだよな~なんでだろ~」で解決方法を見つけたので紹介したいと思います。

再現

ymlファイルを用意しました。

docker-compose.yml
version: '3.9'
services:
  node:
    image: node:18-slim
    tty: true
    ports: 
      - '4000:4000'
    volumes:
     - ./node:/node

コンテナを起動して状態を見てみる。

root@sun33 /home/sun33/qiita/init # docker compose up -d
[+] Running 2/2
 ⠿ Network init_default   Created                   0.0s
 ⠿ Container init-node-1  Started                   0.4s
root@sun33 /home/sun33/qiita/init # docker-compose ps
NAME                COMMAND                  SERVICE             STATUS              PORTS
init-node-1         "docker-entrypoint.s…"   node                running             0.0.0.0:8000->8000/tcp, :::8000->8000/tcp

コンテナを破壊します。

root@sun33 /home/sun33/qiita/init # docker compose down
[+] Running 2/2
 ⠿ Container init-node-1  Removed                 10.3s
 ⠿ Network init_default   Removed                  0.2s
root@sun33 /home/sun33/qiita/init #

十秒もかかるなんかおかしい。なんだろう強制終了した感じ?

原因

PID 1 問題のようでうまく終了シグナルが送れていないのが原因っぽい。
以下の記事がとてもわかりやすかったです。

解決方法

initを追記する!

docker-compose.yml
version: '3.9'
services:
  node:
    image: node:18-slim
    tty: true
    init: true
    ports: 
      - '4000:4000'
    volumes:
     - ./node:/node

コンテナをもう一度破壊して見ます。

root@sun33 /home/sun33/qiita/init # docker compose down
[+] Running 2/2
 ⠿ Container init-node-1  Removed                  0.3s
 ⠿ Network init_default   Removed                  0.2s
root@sun33 /home/sun33/qiita/init #

正常に破壊できました。

まとめ

今まで待ってた10秒何だったんだってぐらい早くなりました。
nodeコンテナ2つ使ってたので毎回20秒待たされていました…
「あれ、この挙動おかしい?」って疑問に思ったら、調べることが重要ですね。おしまい。

92
82
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
92
82

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?