LoginSignup
2
1

More than 1 year has passed since last update.

Localstack の initScripts が完了してから 他のコンテナを起動したい @docker-compose

Last updated at Posted at 2021-09-17

これは

CI等 で LocalStack を使ってテストしたいが、docker-entrypoint-initaws.d に置いた init スクリプトの実行は終わってからテスト実行したい。
そんな人にオススメの適当な方法です。

ざっくりどうするか

  1. docker-compose の healthcheck を書く
    localstack の http://localhost:4566/healthinitScripts: initialized があるかどうかを判定するようにする

  2. depends_on の condition を使う
    service_healthy かどうかを判定する

これだけです。

サンプルコード

このケースは init.sh で cloudformation を実行したあとに app を起動したいケースを想定して書いてます。

./localstack/init/00_init.sh
#!/bin/sh
cd /
awslocal cloudformation create-stack \
  --endpoint-url http://localhost:4566 \
  --stack-name test\
  --template-body file://aws-cloud-formation/cf.yaml
docker-compose.yml
version: '3.8'
services:
  app:
    build:
      dockerfile: Dockerfile
      context: .
    depends_on:
      # IDE に Array is required と怒られるかもしれませんがちゃんと動きます
      localstack:
        condition: service_healthy

  localstack:
    image: localstack/localstack
    volumes:
      - ./aws-cloud-formation:/aws-cloud-formation
      - ./localstack/init:/docker-entrypoint-initaws.d
    ports:
      - 4566:4566
      - 4571:4571
    healthcheck:
      # init スクリプトが完了する前に app が起動しないよう ヘルスチェックする
      test: ["CMD-SHELL", "curl http://localhost:4566/health | grep '\"initScripts\": \"initialized\"'"]
      interval: 2s
      start_period: 20s
      retries: 30
      timeout: 30s

まとめ

色々方法はあるようですが、割と手軽にできたかと思います。
タイミングによってうまく行かないことがある(unhealthyだったからappは起動しない)ので、
もう少し調査が必要そうですが、こうすれば大丈夫!/これで問題ないよ!など知見のある方コメント頂けますとです。

追記

そもそも現行の docker-compose では depends_on にこの形式はサポートされてない・・・?

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