问题
I'm working with a docker service using docker-compose, and I have a service that depends on anther.
I've used the depends_on
key, but the service with the dependency launches prior to the depending service being completely up.
version: '3'
services:
KeyManager:
image: cjrutherford/keymanager
deploy:
replicas: 1
ports:
- '3220:3220'
networks:
- privnet
YellowDiamond:
image: cjrutherford/server
depends_on:
- KeyManager
deploy:
replicas: 1
ports:
- '3000:3000'
networks:
- privnet
- web
networks:
privnet:
internal: true
web:
Both of these are node applications, and the keymanager is required to be running to accept requests before the server launches. Can I add a timeout? or send a trigger in the app? it's just launching way too early to get the key from the manager.
回答1:
I've often found using a wait-for-it bash script much more effective than the built in health check to docker-compose.
This runs a TCP health check against a given port and waits until this is complete before starting to run a process.
Sample code:
version: "2"
services:
web:
build: .
ports:
- "80:8000"
depends_on:
- "db"
command: ["./wait-for-it.sh", "db:5432", "--", "python", "app.py"]
db:
image: postgres
Here's some docs:
- https://docs.docker.com/compose/startup-order/
- https://github.com/vishnubob/wait-for-it
回答2:
You are probably looking for docker compose healthcheck
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 10s
timeout: 5s
retries: 5
There is a good reference here as well:
https://github.com/peter-evans/docker-compose-healthcheck
来源:https://stackoverflow.com/questions/55502234/docker-compose-wait-til-dependency-container-is-fully-up-before-launching