'Creating a new queue in RabbitMQ through docker-compose

Just sharing how I did to create automatically queues when RabbitMq starts:

version: "3.2"
services:
  rabbitmq:
    image: rabbitmq:3-management-alpine
    environment:
      RABBITMQ_DEFAULT_USER: guest
      RABBITMQ_DEFAULT_PASS: guest
    container_name: rabbitmq
    ports:
      - 5672:5672
      - 15672:15672
    volumes:
      - ./queues/rabbitmq/init.sh:/init.sh
    restart: "no"
    entrypoint: [ "bash", "-c", "sleep 3 && ./init.sh"]
    networks:
      abinbev_net:
        ipv4_address: 173.101.101.101

networks:
  abinbev_net:
    driver: bridge
    ipam:
      driver: default
      config:
        - subnet: 173.101.101.0/24

And my init.sh

#!/bin/bash
echo "Generating a new queue through commandline"
#!/bin/sh

# Create Rabbitmq user
( sleep 5 ; \
rabbitmqctl add_user $RABBITMQ_USER $RABBITMQ_PASSWORD 2>/dev/null ; \
rabbitmqctl set_user_tags $RABBITMQ_USER administrator ; \
rabbitmqctl set_permissions -p / $RABBITMQ_USER  ".*" ".*" ".*" ; \
rabbitmqadmin -u guest -p guest -V / declare queue name=otc-finance-reference-receive-queue ; \
echo "*** User '$RABBITMQ_USER' with password '$RABBITMQ_PASSWORD' completed. ***" ; \
echo "*** Log in the WebUI at port 15672 (example: http:/localhost:15672) ***") &

# $@ is used to pass arguments to the rabbitmq-server command.
# For example if you use it like this: docker run -d rabbitmq arg1 arg2,
# it will be as you run in the container rabbitmq-server arg1 arg2
rabbitmq-server $@

So it's working fine but I'd like to know if you have another shape to make that. Thanks, Raphael



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source