Rinat Mukhamedgaliev
Rinat Mukhamedgaliev

Reputation: 5721

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?

Upvotes: 61

Views: 54495

Answers (2)

k4st0r42
k4st0r42

Reputation: 1252

Same issue with swam

The solution is to set the env RABBITMQ_NODENAME=rabbit@my-rabbit And add my-rabbit=127.0.0.1 as extra hosts file entries

Upvotes: 0

wileymab
wileymab

Reputation: 1329

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

Upvotes: 47

Related Questions