In my case, I wonder if I can dump the docker container user/vhost/data by simply mounting the data folder.
I found the following documentation: https://www.rabbitmq.com/backup.html, which helps too much.
For now, I do mount the /var/lib/rabbitmq
volume to the host, but when the container is recreated, the configurations of user and vhosts gone away.
Soom I realized that after a recreation of the container, a new dataset is generated, with a different id.
So the old data is still there, but the id is disconnected.
In my example, the 0df72ae1a7a5
one is old, when I create a new one 268bac197c69
, the old data is not active any more.
root@268bac197c69:~/mnesia# ls -alh /var/lib/rabbitmq/mnesia
total 100K
drwxr-xr-x. 14 rabbitmq rabbitmq 4.0K Jun 13 13:43 .
drwxr-xr-x. 5 rabbitmq root 4.0K Jun 13 13:42 ..
drwxr-xr-x. 4 rabbitmq rabbitmq 4.0K Mar 6 2020 rabbit@0df72ae1a7a5
-rw-r--r--. 1 rabbitmq rabbitmq 64 Mar 6 2020 rabbit@0df72ae1a7a5-feature_flags
drwxr-xr-x. 2 rabbitmq rabbitmq 4.0K Mar 6 2020 rabbit@0df72ae1a7a5-plugins-expand
-rw-r--r--. 1 rabbitmq rabbitmq 2 Mar 6 2020 rabbit@0df72ae1a7a5.pid
drwxr-xr-x. 4 rabbitmq rabbitmq 4.0K Jun 13 13:43 rabbit@268bac197c69
-rw-r--r--. 1 rabbitmq rabbitmq 148 Jun 13 13:43 rabbit@268bac197c69-feature_flags
drwxr-xr-x. 10 rabbitmq rabbitmq 4.0K Jun 13 13:43 rabbit@268bac197c69-plugins-expand
-rw-r--r--. 1 rabbitmq rabbitmq 3 Jun 13 13:43 rabbit@268bac197c69.pid
In the container, the following command shows the current active id:
rabbitmqctl eval 'rabbit_mnesia:dir().'
It prints "/var/lib/rabbitmq/mnesia/rabbit@268bac197c69"
, the current newly created one.
So now the problem is now reduced to:
How to restore the old data with the specific old id when the container recreates?
Soon, I found the current id is the same with the container hostname, which is randomly generated when the container created!
So how the stick the id with a specific value? I check the docker-hub rabbitmq page: https://hub.docker.com/_/rabbitmq
One of the important things to note about RabbitMQ is that it stores data based on what it calls the "Node Name", which defaults to the hostname. What this means for usage in Docker is that we should specify -h/--hostname explicitly for each daemon so that we don't get a random hostname and can keep track of our data:
So comes the final solution, we just need to specify the hostname
to a specific value, everything will come back automatically when the container recreates.
Final Solution:
Just add the hostname setting in our docker-compose section:
Notice: The hostname line and volumes line matters.
rabbitmq:
image: rabbitmq:management
container_name: rabbitmq
restart: always
hostname: 0df72ae1a7a5
environment:
RABBITMQ_DEFAULT_USER: rabbit
RABBITMQ_DEFAULT_PASS: rabbit
volumes:
- /var/docker/rabbitmq/var/lib/rabbitmq:/var/lib/rabbitmq