Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker RabbitMQ persistency

RabbitMQ in docker lost data after remove container without volume.

My Dockerfile:

FROM rabbitmq:3-management ENV RABBITMQ_HIPE_COMPILE 1 ENV RABBITMQ_ERLANG_COOKIE "123456" ENV RABBITMQ_DEFAULT_VHOST "123456" 

My run script:

IMAGE_NAME="service-rabbitmq" TAG="${REGISTRY_ADDRESS}/${IMAGE_NAME}:${VERSION}"  echo $TAG  docker rm -f $IMAGE_NAME    docker run \     -itd \     -v "rabbitmq_log:/var/log/rabbitmq" \     -v "rabbitmq_data:/var/lib/rabbitmq" \     --name "service-rabbitmq" \     --dns=8.8.8.8 \     -p 8080:15672 \     $TAG 

After removing the container, all data are lost.

How do I configure RabbitMQ in docker with persistent data?

like image 437
Rinat Mukhamedgaliev Avatar asked Dec 26 '16 11:12

Rinat Mukhamedgaliev


People also ask

Is docker run persistent?

Docker has two options for containers to store files on the host machine, so that the files are persisted even after the container stops: volumes, and bind mounts. Docker also supports containers storing files in-memory on the host machine. Such files are not persisted.

Is RabbitMQ a docker?

The RabbitMQ container registry (link) includes a variety of images for different platforms. In this article, we will use the RabbitMQ image with a tag 3-management , which is a Docker image with the RabbitMQ management plugin installed and enabled by default.

How do I connect to RabbitMQ in docker container?

If you open your Docker engine, you will see the RbbitMQ container set and running. If you open http://localhost:15672/ on a browser, you will be able to access the management Ui, and now you can log in using the docker-compose set username and password. And now you can see the RabbitMQ instance is up and running.

How do I run RabbitMQ locally docker?

Open a terminal, navigate to your rabbitmq-go folder and run docker-compose up . This command will pull the rabbitmq:3-management-alpine image, create the container rabbitmq and start the service and webUI. You should see something like this: Once you see this, open your browser and head over to http://localhost:15672.


2 Answers

Rabbitmq uses the hostname as part of the folder name in the mnesia directory. Maybe add a --hostname some-rabbit to your docker run?

I had the same issue and I found the answer here.

like image 159
Pedro Mázala Avatar answered Sep 29 '22 21:09

Pedro Mázala


TL;DR

Didn't do too much digging on this, but it appears that the simplest way to do this is to change the hostname as Pedro mentions above.

MORE INFO:

Using RABBITMQ_NODENAME

If you want to edit the RABBITMQ_NODENAME variable via Docker, it looks like you need to add a hostname as well since the Docker hostnames are generated as random hashes.

If you change the RABBITMQ_NODENAME var to something static like my-rabbit, RabbitMQ will throw something like an "nxdomain not found" error because it's looking for something like
my-rabbit@<docker_hostname_hash>. If you know the Docker hostname and can automate pulling it into your RABBITMQ_NODENAME value like so, my-rabbit@<docker_hostname_hash> I believe it would work.


UPDATE

I previously said,

If you know the Docker hostname and can automate pulling it into your RABBITMQ_NODENAME value like so, my-rabbit@<docker_hostname_hash> I believe it would work.

This would not work as described precisely because the default docker host name is randomly generated at launch, if it is not assigned explicitly. The hurdle would actually be to make sure you use the EXACT SAME <docker_hostname_hash> as your originating run so that the data directory gets picked up correctly. This would be a pain to implement dynamically/robustly. It would be easiest to use an explicit hostname as described below.


The alternative would be to set the hostname to a value you choose -- say, app-messaging -- AND ALSO set the RABBITMQ_NODENAME var to something like rabbit@app-messaging. This way you are controlling the full node name that will be used in the data directory.

Using Hostname

(Recommended)

That said, unless you have a reason NOT to change the hostname, changing the hostname alone is the simplest way to ensure that your data will be mounted to and from the same point every time.

I'm using the following Docker Compose file to successfully persist my setup between launches.

version: '3' services:   rabbitmq:     hostname: 'mabbit'     image: "${ARTIFACTORY}/rabbitmq:3-management"     ports:       - "15672:15672"       - "5672:5672"     volumes:       - "./data:/var/lib/rabbitmq/mnesia/"     networks:       - rabbitmq  networks:   rabbitmq:     driver: bridge 

This creates a data directory next to my compose file and persists the RabbitMQ setup like so:

./data/   rabbit@mabbit/   rabbit@mabbit-plugins-expand/   [email protected]   rabbit@mabbit-feature_flags  
like image 34
wileymab Avatar answered Sep 29 '22 21:09

wileymab