LoginSignup
4
2

More than 3 years have passed since last update.

docker の healthcheck でコンテナのサービスが開始されるのを待つ

Last updated at Posted at 2020-12-22

Overview

docker-compose の depends_on でコンテナの依存関係を指定すると、コンテナの起動順序が制御できるけど、
立ち上がったコンテナがちゃんと動いてるか?までは担保してくれないので、
「ミドルウェアのコンテナがちゃんと起動してるかどうかのチェックは、wait-for-it とかでアプリケーション側で頑張ってね」的なお困り事案よくあるじゃないですか。
https://docs.docker.jp/compose/startup-order.html

で、 docker に healthcheck 機能があって、
https://dev.classmethod.jp/articles/docker-healthcheck/
これを使うと依存してるコンテナが healthy になるまで起動を待たせられるっていう話。

試してみる

alpine に redis 入れて試してみます。

Dockerfile
FROM alpine:latest

RUN apk add make redis
Makefile
node1:    # 待たされる側
    @ echo "$@ is running!"
    redis-cli -h node2 ping

node2:    # 待たせる側
    @ echo "$@ started!"
    sleep 3    # 3秒待ってから
    redis-server --protected-mode no    # redis-server を起動
    @ echo "$@ ended!"
docker-compose.yml
version: '3'

services:
  node1:
    image: hoge
    build:
      context: .
    command: make -f /mnt/Makefile node1
    depends_on:
      node2:    # node2 の healthcheck が通るまで起動を待つ
        condition: service_healthy
    volumes:
      - .:/mnt

  node2:
    image: hoge
    build:
      context: .
    command: make -f /mnt/Makefile node2
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 1s
      timeout: 30s
      retries: 30
    volumes:
      - .:/mnt

これで

bash
$ docker-compose build

して

bash
$  docker-compose up

すると、 node2 側で redis の起動前に 3秒 sleep してる間は healthcheck の redis-cli ping が通らないので、
node1 の起動も待たされる挙動が確認できます。

こんな感じ
$ docker-compose up
Creating network "docker_default" with the default driver
Creating docker_node2_1 ... done
Creating docker_node1_1 ... done
Attaching to docker_node2_1, docker_node1_1
node2_1  | node2 is started!
node2_1  | sleep 3    # 3秒待ってから
node1_1  | node1 is running!
node1_1  | redis-cli -h node2 ping
node2_1  | redis-server --protected-mode no    # redis-server を起動
node2_1  | 22:C 22 Dec 2020 09:38:25.036 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
node2_1  | 22:C 22 Dec 2020 09:38:25.036 # Redis version=5.0.9, bits=64, commit=869dcbdc, modified=0, pid=22, just started
node2_1  | 22:C 22 Dec 2020 09:38:25.036 # Configuration loaded
node1_1  | PONG
node2_1  | 22:M 22 Dec 2020 09:38:25.039 * Running mode=standalone, port=6379.
node2_1  | 22:M 22 Dec 2020 09:38:25.039 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
node2_1  | 22:M 22 Dec 2020 09:38:25.039 # Server initialized
node2_1  | 22:M 22 Dec 2020 09:38:25.039 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
node2_1  | 22:M 22 Dec 2020 09:38:25.040 * Ready to accept connections
docker_node1_1 exited with code 0

っていう話。

cf.

4
2
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
4
2