How can we control the startup order of services in Docker compose?
How can we control the startup order of services in Docker compose?
To control the startup order of services in Docker Compose, you can use the depends_on
option. This option allows you to specify dependencies between services, ensuring that certain services start before others. However, it is important to note that depends_on
only ensures that the dependent services are started, not that they are fully ready to accept connections.
Here are the key points to consider:
Using depends_on
:
depends_on
option specifies the order in which services are started. For example, if service A depends on service B, service B will start before service A.version: "3"
services:
web:
build: .
depends_on:
- db
db:
image: postgres
Handling Service Readiness:
wait-for-it
, dockerize
, or sh-compatible wait-for
to poll a given host and port until it is accepting TCP connections[1][2][3][4].Example with wait-for-it.sh
:
version: "3"
services:
web:
build: .
depends_on:
- db
command: ["./wait-for-it.sh", "db:5432", "--", "python", "app.py"]
db:
image: postgres
Custom Wrapper Script:
wait-for-postgres.sh
):
#!/bin/sh
set -e
host="$1"
...
middle
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào