Docker Community Forums

Share and learn in the Docker community.

How to set delay to for a ready container in docker

I have a service using replicas mode called A, another service called B connect to service A. When I increase replicas from service A, at service B I get 502 or 503 error because service A needs x seconds to be ready. Whether can I set delay time for instance to be ready and used by service B?

1 Like

With docker-compose you can use depends_on.

A cleaner approach is to implement a wait-for in your entrypoint script of B and wait for A beeing available before you start the service in B. This is one of the many implementations of wait-for:

I think there are some mistakes here. I mean:

– Service A and B are running
– Service B depends on A
– Service A use relicated mode
– Service A want to increase replicas

And I think wait-for is used in case service B isn’t started

1 Like

Let me retry. Your problem is that service B gets 50x errors, because replica instances of service A are started but the service is not usable yet.

With docker compose, you can use healthcheck on service A.

And reference it in service B like this

        condition: service_healthy

On Swarm: you are on your own! Make sure you build your software with resiliancy build-in: expect unreliable connections, have retries with curcuit breakers.

On Kubernetes: has probes that allow to express exactly what you want.


Thank you so much!

        condition: service_healthy

just can be used in docker compose version 2 and I am using healthcheck with version 3

Are you using docker-compose or swarm? Version 2 is ment for docker-compose and version 3 for swarm. There is no benefit in using version 3 for docker-compose, only drawbacks.

1 Like

The Healthchk cmd does have a --start-period=DURATION(default:0s` ) setting that can be used to give the container some time to initialize itself before healthchk failures are counted against it.