docker-compose healthcheck for rabbitMQ

8,542

Solution 1

You could use the command rabbitmq-diagnostics -q ping in case you just need a basic check.

healthcheck:
  test: rabbitmq-diagnostics -q ping
  interval: 30s
  timeout: 30s
  retries: 3

More information on how to run more advanced health checks could be found here

Solution 2

If rabbitmq-diagnostics -q ping not working for you, check this

healthcheck:
  test: rabbitmq-diagnostics check_port_connectivity
  interval: 1s
  timeout: 3s
  retries: 30

In my example I also reduced interval.

Share:
8,542
Wine
Author by

Wine

Updated on September 18, 2022

Comments

  • Wine
    Wine almost 2 years

    I'm trying to run rabbitMQ using docker-compose, but the service is always starting or unhealthy.
    rabbit is running fine, so I suspect there is something wrong with my health check.

    Running the healthcheck command locally does return a value.

    > curl -f http://localhost:5672
    AMQP    %
    

    But docker-compose ps always says the service is unhealthy (or starting, before it runs out of time).

    > docker-compose ps
    docker-entrypoint.sh rabbi ...   Up (unhealthy)   15671/tcp
    

    Here is what my docker-compose.yml file looks like.

    # docker-compose.yml
    version: '2.3' # note: I can't change this version, must be 2.3
    
    volumes:
      rabbit-data:
    
    services:
      rabbit:
        hostname: 'rabbit'
        image: rabbitmq:3.8.5-management
        healthcheck:
          test: ["CMD", "curl", "-f", "http://localhost:5672"]
          interval: 30s
          timeout: 30s
          retries: 3
        ports:
          - '5672:5672'
          - '15672:15672'
        volumes:
          - 'rabbit-data:/var/lib/rabbitmq/mnesia/'
        networks:
          - rabbitmq
    
    networks:
      rabbitmq:
        driver: bridge
    

    I have also tried using nc instead of curl in the healthcheck, but got the same result.

    healthcheck:
      test: [ "CMD", "nc", "-z", "localhost", "5672" ]
    

    From https://github.com/docker-library/rabbitmq/issues/326

  • Wine
    Wine almost 4 years
    Thanks a million, that's exactly what I needed! The docs mention each stage builds on the previous. >"This includes the stage 1 check plus ..." Do you know if you have to chain the commands? or does each command implicitly cover the previous one? e.g. which do I need for stage 2? 1. rabbitmq-diagnostics -q ping && rabbitmq-diagnostics -q status 2. rabbitmq-diagnostics -q status
  • pmodernell
    pmodernell almost 4 years
    Option 2 should be enough. Every time the status command returns OK, the ping command would have been successful too. But, the status makes some additional checks.