🌈

docker composeでdockerizeやwait-for-itなしでmysqlの起動を待つ

2023/06/13に公開2

モチベーション

dockerizewait-for-itを使わずにmysqlの起動を待ちたい

depends_onとhealthcheckを使う

depends_onのconditionにservice_healthyを指定するとできます

ドキュメントは以下の通り
depends_on
healthcheck

実際のファイル

compose.yaml
services:
  server:
    container_name: awesome-server
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - '3000:3000'
    depends_on: # <- ここ
      awesome-mysql:
        condition: service_healthy

  mysql:
    container_name: awesome-mysql
    image: mysql:8.0
    environment:
      MYSQL_DATABASE: hello
      MYSQL_ROOT_PASSWORD: password
    ports:
      - '3306:3306'
    healthcheck: # <- ここ
      test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]
      timeout: 20s
      retries: 10

これでdockerizeやwait-for-itなしでmysqlの起動を待てるようになった!

GitHubで編集を提案
ispec

Discussion