NET=${NET:-rabbit-net}
# Create a network
docker network create -d bridge --subnet 172.25.0.0/16 $NET
# Start containers
docker rm -f rabbit1 rabbit2 >/dev/null 2>&1
# Start rabbit1
docker run -d -p 5672:5672 -p 5671:5671 -p 15672:15672 -e HOSTNAME=rabbit1 --name=rabbit1 --net=$NET docker.example.com/rabbitmq:3.6.2
# Start rabbit2, which clusters with rabbit1
docker run -d -p 6672:5672 -p 6671:5671 -p 16672:15672 -e HOSTNAME=rabbit2 --name=rabbit2 --net=$NET -e CLUSTER_WITH=rabbit1 docker.example.com/rabbitmq:3.6.2
The container images are identical, but rabbit2 is configured via the environment to cluster with rabbit1. Here is the entrypoint for both containers:
rabbitmq-server -detached
sleep 5
rabbitmq-plugins enable rabbitmq_management
rabbitmqctl add_user remote sekrit
rabbitmqctl set_user_tags remote administrator
rabbitmqctl set_permissions -p / remote ".*" ".*" ".*"
CLUSTER_WITH=${CLUSTER_WITH:-nobody}
if [ $CLUSTER_WITH != "nobody" ]; then
until nmap -p5672 $CLUSTER_WITH | grep ^5672.*open
do
echo "Waiting for $CLUSTER_WITH listener"
done
rabbitmqctl stop_app
sleep 3
rabbitmqctl join_cluster rabbit@${CLUSTER_WITH}
sleep 3
rabbitmqctl start_app
fi
set +x
while true ; do sleep 5; done