├── demo.gif ├── .github └── workflows │ └── push.yml ├── LICENSE ├── docker-compose.yml ├── README_JP.md └── README.md /demo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peter-evans/docker-compose-healthcheck/HEAD/demo.gif -------------------------------------------------------------------------------- /.github/workflows/push.yml: -------------------------------------------------------------------------------- 1 | name: docker-compose-healthcheck 2 | on: push 3 | jobs: 4 | test: 5 | runs-on: ubuntu-latest 6 | steps: 7 | - uses: actions/checkout@v2 8 | - name: Bring up the stack 9 | run: docker-compose up -d 10 | - name: Test endpoint 11 | run: docker run --network container:kong appropriate/curl -s -4 --retry 10 --retry-connrefused http://localhost:8001/ 12 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Peter Evans 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3.9' # optional since Compose v1.27.0 2 | 3 | services: 4 | kong-database: 5 | image: postgres:9.5 6 | container_name: kong-postgres 7 | environment: 8 | - POSTGRES_USER=kong 9 | - POSTGRES_DB=kong 10 | - POSTGRES_HOST_AUTH_METHOD=trust 11 | healthcheck: 12 | test: ["CMD-SHELL", "pg_isready"] 13 | interval: 10s 14 | timeout: 5s 15 | retries: 5 16 | 17 | kong-migration: 18 | image: kong 19 | container_name: kong-migration 20 | depends_on: 21 | kong-database: 22 | condition: service_healthy 23 | environment: 24 | - KONG_DATABASE=postgres 25 | - KONG_PG_HOST=kong-database 26 | command: kong migrations bootstrap 27 | 28 | kong: 29 | image: kong 30 | container_name: kong 31 | restart: always 32 | depends_on: 33 | kong-database: 34 | condition: service_healthy 35 | kong-migration: 36 | condition: service_started 37 | links: 38 | - kong-database:kong-database 39 | ports: 40 | - 8000:8000 41 | - 8443:8443 42 | - 8001:8001 43 | - 8444:8444 44 | environment: 45 | - KONG_DATABASE=postgres 46 | - KONG_PG_HOST=kong-database 47 | - KONG_PG_DATABASE=kong 48 | - KONG_ADMIN_LISTEN=0.0.0.0:8001 49 | -------------------------------------------------------------------------------- /README_JP.md: -------------------------------------------------------------------------------- 1 | # docker-compose-healthcheck 2 | [![Actions Status](https://github.com/peter-evans/docker-compose-healthcheck/workflows/docker-compose-healthcheck/badge.svg)](https://github.com/peter-evans/docker-compose-healthcheck/actions) 3 | 4 | Docker Composeの[バージョン2.1のファイルフォーマット](https://docs.docker.com/compose/compose-file/compose-versioning/#version-21)が作成して以来、[healthcheck](https://docs.docker.com/compose/compose-file/#healthcheck)パラメータが導入されました。 5 | これにより、サービスのコンテナが「healthy」(正常)であるかどうかを判断するためのチェックを構成できます。 6 | 7 | ## コンテナYを開始する前にコンテナXが正常になるまで待機する方法は? 8 | 9 | これは一般的な問題であり、以前のバージョンのdocker-composeでは[wait-for-it](https://github.com/vishnubob/wait-for-it)や[dockerize](https://github.com/jwilder/dockerize)などのツールやスクリプトを使用する必要があります。 10 | `healthcheck`パラメータを使うと、これらのツールやスクリプトの使用はもはや必要ではないことがよくあります。 11 | 12 | ## PostgreSQLが「healthy」(正常)になるまで待機する方法 13 | 14 | 特によく使用されるケースは、PostgreSQLなどのデータベースに依存するサービスです。 15 | PostgreSQLコンテナが起動し、リクエストを受け入れる準備ができたら続行するという設定ができます。 16 | 17 | 以下の例では、`pg_isready`コマンドを使用してPostgreSQLが使用可能かどうかを定期的にチェックするように設定されています。[`pg_isready`コマンドの参照](https://www.postgresql.org/docs/9.4/static/app-pg-isready.html) 18 | ```yml 19 | healthcheck: 20 | test: ["CMD-SHELL", "pg_isready"] 21 | interval: 10s 22 | timeout: 5s 23 | retries: 5 24 | ``` 25 | チェックが成功すると、コンテナは「正常」となります。それまでは、`unhealthy`(障害)にとどまります。 26 | healthcheckパラメータ `interval`、` timeout`、 `retries`の詳細については、[ドキュメンテーション](https://docs.docker.com/engine/reference/builder/#healthcheck)を参照してください。 27 | 28 | それで、PostgreSQLに依存するサービスは `depends_on`パラメータで設定することができます。 29 | ```yml 30 | depends_on: 31 | postgres-database: 32 | condition: service_healthy 33 | ``` 34 | 35 | ## Kongを開始する前にPostgreSQLが正常になるまで待機 36 | 37 | [この完全な例では](docker-compose.yml) docker-composeがオープンソースのAPIゲートウェイである[Kong](https://getkong.org/)を起動する前にPostgreSQLが「healthy」(正常)になるまで待機します。 38 | また、もう一つの一時的なコンテナがKongのデータベース移行プロセスを完了するのを待ちます。 39 | 40 | 以下のようにテストします: 41 | ``` 42 | docker-compose up -d 43 | ``` 44 | 45 | 全サービスが実行されるまで待ちます: 46 | 47 | ![Demo](/demo.gif?raw=true) 48 | 49 | Kongの管理エンドポイントをクエリしてテストします: 50 | ``` 51 | curl http://localhost:8001/ 52 | ``` 53 | 54 | ## ライセンス 55 | 56 | MIT ライセンス - 詳細については[LICENSE](LICENSE)ファイルを見てください 57 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # docker-compose-healthcheck [The blog of Peter Evans: How to Wait for Container X Before Starting Y](https://peterevans.dev/posts/how-to-wait-for-container-x-before-starting-y/) 2 | [![Actions Status](https://github.com/peter-evans/docker-compose-healthcheck/workflows/docker-compose-healthcheck/badge.svg)](https://github.com/peter-evans/docker-compose-healthcheck/actions) 3 | 4 | The [healthcheck](https://docs.docker.com/compose/compose-file/compose-file-v2/#healthcheck) property was originally introduced in the 2.1 Compose file format and is now part of the [Compose Specification](https://github.com/compose-spec/compose-spec/blob/master/spec.md) used by recent versions of Docker Compose. 5 | This allows a check to be configured in order to determine whether or not containers for a service are "healthy." 6 | 7 | ## How can I wait for container X before starting Y? 8 | 9 | This is a common problem and in earlier versions of docker-compose requires the use of additional tools and scripts such as [wait-for-it](https://github.com/vishnubob/wait-for-it) and [dockerize](https://github.com/jwilder/dockerize). 10 | Using the `healthcheck` parameter the use of these additional tools and scripts is often no longer necessary. 11 | 12 | ## Waiting for PostgreSQL to be "healthy" 13 | 14 | A particularly common use case is a service that depends on a database, such as PostgreSQL. 15 | We can configure docker-compose to wait for the PostgreSQL container to startup and be ready to accept requests before continuing. 16 | 17 | The following healthcheck has been configured to periodically check if PostgreSQL is ready using the `pg_isready` command. See the documentation for the `pg_isready` command [here](https://www.postgresql.org/docs/9.4/static/app-pg-isready.html). 18 | ```yml 19 | healthcheck: 20 | test: ["CMD-SHELL", "pg_isready"] 21 | interval: 10s 22 | timeout: 5s 23 | retries: 5 24 | ``` 25 | If the check is successful the container will be marked as `healthy`. Until then it will remain in an `unhealthy` state. 26 | For more details about the healthcheck parameters `interval`, `timeout` and `retries` see the documentation [here](https://docs.docker.com/engine/reference/builder/#healthcheck). 27 | 28 | Services that depend on PostgreSQL can then be configured with the `depends_on` parameter as follows: 29 | ```yml 30 | depends_on: 31 | postgres-database: 32 | condition: service_healthy 33 | ``` 34 | 35 | ## Waiting for PostgreSQL before starting Kong 36 | 37 | In [this complete example](docker-compose.yml) docker-compose waits for the PostgreSQL service to be "healthy" before starting [Kong](https://getkong.org/), an open-source API gateway. It also waits for an additional ephemeral container to complete Kong's database migration process. 38 | 39 | Test it out with: 40 | ``` 41 | docker-compose up -d 42 | ``` 43 | Wait until all services are running: 44 | 45 | ![Demo](/demo.gif?raw=true) 46 | 47 | Test by querying Kong's admin endpoint: 48 | ``` 49 | curl http://localhost:8001/ 50 | ``` 51 | 52 | ## License 53 | 54 | MIT License - see the [LICENSE](LICENSE) file for details 55 | --------------------------------------------------------------------------------