简体   繁体   中英

Export RabbitMQ Docker image with vhost and queues

I have a rabbitMQ docker container that I started using the following command:

docker run -d --name myrabbit1 -p 15672:15672 rabbitmq:3-management

I then loggin to the management plugin and create users, vhosts, queues, etc.

I want to save all those settings so they can be loaded up again. To do that I tried committing to a new image:

docker commit myrabbit1 vbrabbit:withVhostAndQueues

I then start up my new container (after stopping the old one):

docker run -d --name vbrabbit2 -p 15672:15672 -p 5672:5672 vbrabbit:withVhostAndQueues

I expect that all the queues, vhosts, etc would be saved, but they are not.

What am I missing?

Result from docker ps -a : 在此处输入图像描述

I want to save all those settings so they can be loaded up again

are you needing to create a copy of the container, with the same settings?

or are you just looking to docker stop myrabbit1 and then later docker start myrabbit to run the same container, again?

TL;DR

The RabbitMQ instance within the container is looking for data in a different place. The default configuration changes the data storage/load location per container creation. Thus the OPs data existed in the created "final" image but rabbitmq wasn't loading it.

To fix statically set RABBITMQ_NODENAME which likewise might requiring adding another line to /etc/hosts for RabbitMQ to affirm the node is active.

Details

This happened to me with docker rabbit:3.8.12-management

This is caused by RabbitMQ's default configuration impacting how it does data storage. By default RabbitMQ starts a node on UNIX system with a name of rabbit@$HOSTNAME (see RABBITMQ_NODENAME on config docs ). In Docker the $HOSTNAME changes per container run it defaults to the container id (eg something like dd84759287560 ).

In @jhilden's case is when the vbrabbit:withVhostAndQueues image is booted as a new container the RABBITMQ_NODENAME becomes a different value then what was used to create and store the original vhosts, user, queues, etc. And as RabbitMQ stores data inside a directory named after the RABBITMQ_NODENAME the existing data isn't loaded on boot of vbrabbit:withVhostAndQueues . As when the $HOSTNAME changes the RABBITMQ_NODENAME changes. Thus the booting RabbitMQ instance cannot find any existing data. (eg the existing data is there in the image but for a different RABBITMQ_NODENAME and isn't loaded).

Note: I've only looked into solving this for a local development single instance cluster. If you're using RabbitMQ docker for a production deployment you'd probably need to look into customized hostnames

To fix this issue we set a static RABBITMQ_NODENAME for the container.

In docker-compose v3 file we updated from:

  # Before fix
  rabbitmq:
    image: "rabbitmq:$RABBITMQ_VERSION"
    container_name: rabbitmq
    ports:
      - "5672:5672"
      - "15672:15672"
      - "61613:61613"
    volumes:
      - "./etc/rabbit-plugins:/etc/rabbitmq/enabled_plugins"
      - type: volume
        source: rabbitmq-data
        target: /var/lib/rabbitmq

Into after fix:

  rabbitmq:
    image: "rabbitmq:$RABBITMQ_VERSION"
    container_name: rabbitmq
    # Why do we set NODENAME and extra_hosts?
    # 
    # It codifies that we're using the same RabbitMQ instance between container rebuilds.
    # If NODENAME is not set it defaults to "rabbit@$HOST" and because $HOST is dynamically
    # created in docker it changes per container deployment. Why is a changing host an issue?
    # Well because under the hood Rabbit stores data on a per node basis. Thus without the
    # static RABBITMQ_NODENAME the directory the data is stored within changes per restart.
    # Going from "rabbit@7745942c559e" to "rabbit@036834720485" the next. Okay, but why do we
    # need extra_hosts? We'll Rabbit wants to resolve itself to affirm it's management UI is
    # functioning post deployment and does that with an HTTP call. Thus to resolve the static
    # host from RABBITMQ_NODENAME we need to add it to the containers /etc/hosts file.
    environment:
      RABBITMQ_NODENAME: "rabbit@staticrabbit"
    extra_hosts:
      - "staticrabbit:127.0.0.1"
    ports:
      - "5672:5672"
      - "15672:15672"
      - "61613:61613"
    volumes:
      - "./etc/rabbit-plugins:/etc/rabbitmq/enabled_plugins"
      - type: volume
        source: rabbitmq-data
        target: /var/lib/rabbitmq

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM