あるコンテナが別のコンテナに依存しているケース depends_onで起動を待っているのに、DBが接続を受け付ける前にSQLを叩いて失敗する depends_onはコンテナの起動を待つだけで、起動後にサービスがreadyになるのを待ってくれない
最初に思いつくのはこのへん
例
sleep 10
bundle exec rails db:create
...
例
while true; do
mysqladmin ping -h mysql -u root && break
done
bundle exec rails db:create
...
例
./wait-for-it.sh mysql:3306
mysqladmin ping -h mysql -u root
bundle exec rails db:create
...
ver 2.1から使用可能 特定のコマンドが成功するまで待つことができる
services:
rails:
build:
context: .
depends_on:
mysql:
condition: service_healthy
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:3000/"]
interval: 5s
timeout: 5s
retries: 10
start_period: 30s
mysql:
image: mysql:8.0.32
command:
# this is to match Aurora mysql 2.10.2's empty sql_mode
- --sql-mode=
- --default-authentication-plugin=mysql_native_password
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-u", "root"]
interval: 5s
timeout: 5s
retries: 10
実行するコマンドを書く このコマンドの戻り値が0以外になるまでリトライする
intervalごとにリトライし、retriesの回数失敗したら起動を諦める
開始からstart_periodの間は失敗してもカウントしない (成功したらその時点でhealthyになる)
condition: service_healthy にすることでhealthcheckを待つservice_started (従来の動作)service_completed_successfully もある(対象コンテナの終了を待つ)