0
0

More than 1 year has passed since last update.

Node.js 18以降のSlim/Alpine版Dockerイメージ内からHTTPの疎通確認をワンライナーで行う

Last updated at Posted at 2023-02-03

Node.js公式Dockerイメージのうち、Slim版Alpine版には curlwget入っていない

image.png

よってコンテナ内からHTTPサーバーの疎通確認・死活監視などを行う際にこれらを使った方法は採用できない。

image.png

compose.yaml
services:
  service:
    # ...
    healthcheck:
      test: curl -f http://localhost:3000/ # --> 必ず `Unhealthy` になる
      interval: 5s

しかしNode.js 18以降(フラグ付きなら17.5以降)では fetch が使用できるようになったため、これで代替できる。

image.png

compose.yaml
services:
  service:
    # ...
    healthcheck:
      test: echo "fetch('http://localhost:3000/')" | node
      interval: 5s

ポートが複数の場合はforEachで回せる。

echo "[3000,6006].forEach(p => fetch('http://localhost:' + p))" | node

数珠つなぎにすることもできる。

echo "fetch('http://localhost:3000/');fetch('https://example.com/');" | node

自分自身の死活監視やちょっとしたコンテナ間依存関係の構築用であればこれで十分な場合も多いと思われる。

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