1

My Dockerfile starts the application using the following command

CMD ["/home/app/start-app.sh"]

start-app.sh contains the following, it waits until RabbitMQ server is available. Is there a better way of achieving this for applications running using docker-compose, k8s

while ! nc -z "$RABBITMQ_HOSTNAME" "$SPRING_RABBITMQ_PORT"; do sleep 10; done
Rpj
  • 5,348
  • 16
  • 62
  • 122

1 Answers1

1

I guess not. Even though docker compose provides a depends_on it just starts the containers in the dependency order. It does not make sure that a specific service is available. The docker documentation explains it in this way that I can't do better:

The problem of waiting for a database (for example) to be ready is really just a subset of a much larger problem of distributed systems. In production, your database could become unavailable or move hosts at any time. Your application needs to be resilient to these types of failures.

To handle this, design your application to attempt to re-establish a connection to the database after a failure. If the application retries the connection, it can eventually connect to the database.

You already do what is needed if you have to wait for another service to become ready. But like they say, it is better to develop applications that can handle downtime of other services, because downtime can happen anytime. That's why netflix developed the chaos monkey. One might argue now that handling these possible states makes applications so much harder to develop. That's true, but it's just the complexity of distribution. One can either ignore it: "this will never happen" and I guarantee it will or accept that it can happen and do the right thing. Maybe this decision depends on the risk and the damage it will produce when it happens to your company.

Services or microservices give us a lot of decoupling and flexibility, but they come at a coast that we should keep in mind.

René Link
  • 48,224
  • 13
  • 108
  • 140