一、编写dockerfile文件,配置需要的插件
vim package/dockerfile-rabbitmq.yaml FROM rabbitmq:3.8.5 WORKDIR /var/lib/rabbitmq COPY rabbitmq_delayed_message_exchange-3.8.0.ez /opt/rabbitmq/plugins/ RUN chown rabbitmq:rabbitmq /opt/rabbitmq/plugins/rabbitmq_delayed_message_exchange-3.8.0.ez RUN chmod 644 /opt/rabbitmq/plugins/rabbitmq_delayed_message_exchange-3.8.0.ez RUN rabbitmq-plugins enable --offline rabbitmq_delayed_message_exchange RUN rabbitmq-plugins enable --offline rabbitmq_management RUN rabbitmq-plugins enable --offline rabbitmq_web_stomp RUN rabbitmq-plugins enable --offline rabbitmq_web_mqtt
二、编写docker-compose文件,设置需要持久化的目录
vim docker-compose-rabbitmq.yaml version: "3.3" networks: default: external: true name: learn-network services: learn-rabbitmq: image: learn/rabbitmq:3.8.5 hostname: rabbitmq container_name: learn-rabbitmq restart: always ports: - "6672:6672" - "15672:15672" - "25672:25672" - "15674:15674" - "15675:15675" - "1883:1883" - "61613:61613" volumes: #- /etc/hosts:/etc/hosts - /etc/localtime:/etc/localtime - ./config/rabbitmq/rabbitmq.conf:/etc/rabbitmq/rabbitmq.conf - ./data/rabbitmq:/var/lib/rabbitmq
这里的hostname用于配置容器主机名,这样才能保证每次容器启动时主机名一致,持久化的mq配置数据库目录是同一个,才能保证数据的持久化
三、docker-compose 启动容器
docker-compose -f docker-compose-rabbitmq.yaml up -d
四、进入容器进行用户和vhost的相关配置
docker exec -it learn-rabbitmq /bin/bash rabbitmqctl add_user hlmq xxxxxxx rabbitmqctl set_user_tags hlmq administrator rabbitmqctl set_permissions -p / hlmq '.*' '.*' '.*' rabbitmqctl add_vhost im rabbitmqctl set_permissions -p im hlmq '.*' '.*' '.*'
配置完后,所有的数据都持久化到宿主机对应的目录下了
五、停止删除容器,并重新启动进入容器验证
docker stop learn-rabbitmq && docker rm -f learn-rabbitmq docker-compose -f docker-compose-rabbitmq.yaml up -d docker exec -it learn-rabbitmq /bin/bash
root@rabbitmq:~# rabbitmqctl list_users
Listing users ...
user tags
hlmq [administrator]
guest [administrator]
标签:compose,plugins,rabbitmq,vhost,rabbitmqctl,mq,learn,docker From: https://www.cnblogs.com/sky-cheng/p/18550001