I have a microservice application that uses RabbitMQ. How can I run the RabbitMQ consumer from the application backend container only after the RabbitMQ is up and running. My compose file is as below.
certichain_backend:
depends_on:
- rabbitmq
working_dir: /app/backend/src
command: sh sleep 20 & nohup node /app/backend/src/services/amqp_consumer.js && npm run start;
rabbitmq:
image: "rabbitmq:3-management"
hostname: "rabbitmq"
restart: always
expose:
- 15672
- 5672
labels:
NAME: "rabbitmq"
volumes:
- ./rabbitmq.conf:/etc/rabbitmq/rabbitmq.conf
I have given the backend the 'depends_on' to rabbitmq. But what I have observed is, the rabbitmq container starting process is initiated. But the backend container is not waiting for the rabbitmq container to up completely. My consumer is running along with the backend. So the consumer cannot connect to amqp server since it is not running at that moment. Thus I added a sleep parameter. So that it gets some time while rabbitmq to bringing up.
This method is very inconsistent. I'm sure this is not the right way to achieve this.