Docker 安装 RabbitMQ,自定义数据卷位置启动失败,分析过程

问题描述
  • 运行docker命令,RabbitMQ启动失败,且没有特别错误日志
docker run -d -p 5672:5672 -p 15672:15672 --name rabbitmq -v /xzlawin/mydocker/rabbitmq/volume/5672/plugins:/opt/rabbitmq/plugins -v /xzlawin/mydocker/rabbitmq/volume/5672/log:/var/log/rabbitmq -v /xzlawin/mydocker/rabbitmq/volume/5672/data:/var/lib/rabbitmq -v /xzlawin/mydocker/rabbitmq/volume/5672/config:/etc/rabbitmq -e RABBITMQ_DEFAULT_USER=guest -e RABBITMQ_DEFAULT_PASS=guest 6c3c2a225947
  • 1.
分析原因
  • 容器内的文件未同步到数据卷映射的目录上,文件缺失,导致启动失败
分析步骤一,运行docker命令,使用默认数据卷位置,启动成功
docker run -d -p 5672:5672 -p 15672:15672 --name rabbitmq 6c3c2a225947
  • 1.
  • 查看容器ID
docker ps
  • 1.
  • 查看数据卷信息
docker inspect [容器ID]
  • 1.

控制台显示

"Mounts": [
            {
                "Type": "volume",
                "Name": "rabbitmq_log",
                "Source": "/var/lib/docker/volumes/rabbitmq_log/_data",
                "Destination": "/var/log/rabbitmq",
                "Driver": "local",
                "Mode": "z",
                "RW": true,
                "Propagation": ""
            },
            {
                "Type": "volume",
                "Name": "rabbitmq_plugins",
                "Source": "/var/lib/docker/volumes/rabbitmq_plugins/_data",
                "Destination": "/opt/rabbitmq/plugins",
                "Driver": "local",
                "Mode": "z",
                "RW": true,
                "Propagation": ""
            },
            {
                "Type": "volume",
                "Name": "rabbitmq_config",
                "Source": "/var/lib/docker/volumes/rabbitmq_config/_data",
                "Destination": "/etc/rabbitmq",
                "Driver": "local",
                "Mode": "z",
                "RW": true,
                "Propagation": ""
            },
            {
                "Type": "volume",
                "Name": "rabbitmq_data",
                "Source": "/var/lib/docker/volumes/rabbitmq_data/_data",
                "Destination": "/var/lib/rabbitmq",
                "Driver": "local",
                "Mode": "z",
                "RW": true,
                "Propagation": ""
            }
        ],
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 删除容器
docker stop [容器ID]
docker ps -a [容器ID]
docker rm -f [容器ID]
  • 1.
  • 2.
  • 3.
分析步骤二,指定默认数据卷位置,启动成功
docker run -d \
--name rabbitmq \
-p 5672:5672 \
-p 15672:15672 \
-e RABBITMQ_DEFAULT_USER=guest \
-e RABBITMQ_DEFAULT_PASS=guest \
-v /var/lib/docker/volumes/rabbitmq_config/_data:/etc/rabbitmq \
-v /var/lib/docker/volumes/rabbitmq_data/_data:/var/lib/rabbitmq \
-v /var/lib/docker/volumes/rabbitmq_log/_data:/var/log/rabbitmq \
-v /var/lib/docker/volumes/rabbitmq_plugins/_data:/opt/rabbitmq/plugins \
rabbitmq:management
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 删除容器
docker stop [容器ID]
docker ps -a [容器ID]
docker rm -f [容器ID]
  • 1.
  • 2.
  • 3.
分析步骤三,指定自定义数据卷位置,启动失败
docker run -d -p 5672:5672 -p 15672:15672 --name rabbitmq -v /xzlawin/mydocker/rabbitmq/volume/5672/plugins:/opt/rabbitmq/plugins -v /xzlawin/mydocker/rabbitmq/volume/5672/log:/var/log/rabbitmq -v /xzlawin/mydocker/rabbitmq/volume/5672/data:/var/lib/rabbitmq -v /xzlawin/mydocker/rabbitmq/volume/5672/config:/etc/rabbitmq -e RABBITMQ_DEFAULT_USER=guest -e RABBITMQ_DEFAULT_PASS=guest 6c3c2a225947
  • 1.
  • 删除容器
docker stop [容器ID]
docker ps -a [容器ID]
docker rm -f [容器ID]
  • 1.
  • 2.
  • 3.
分析步骤四,查看自定义数据卷文件,发现文件没有同步目录为空
ll /xzlawin/mydocker/rabbitmq/volume/5672/plugins
ll /xzlawin/mydocker/rabbitmq/volume/5672/log
ll /xzlawin/mydocker/rabbitmq/volume/5672/data
ll /xzlawin/mydocker/rabbitmq/volume/5672/config
  • 1.
  • 2.
  • 3.
  • 4.
解决方法
  • 将默认数据卷文件,复制到自定义数据卷位置,启动成功
解决步骤一, 将默认数据卷文件,复制到自定义数据卷位置,启动失败
  • 将默认数据卷文件,复制到自定义数据卷位置
cp -r /var/lib/docker/volumes/rabbitmq_log/_data/* /xzlawin/mydocker/rabbitmq/volume/5672/log
cp -r /var/lib/docker/volumes/rabbitmq_plugins/_data/* /xzlawin/mydocker/rabbitmq/volume/5672/plugins
cp -r /var/lib/docker/volumes/rabbitmq_config/_data/* /xzlawin/mydocker/rabbitmq/volume/5672/config
cp -r /var/lib/docker/volumes/rabbitmq_data/_data/* /xzlawin/mydocker/rabbitmq/volume/5672/data
  • 1.
  • 2.
  • 3.
  • 4.
  • 指定数据卷位置,启动失败
docker run -d -p 5672:5672 -p 15672:15672 --name rabbitmq -v /xzlawin/mydocker/rabbitmq/volume/5672/plugins:/opt/rabbitmq/plugins -v /xzlawin/mydocker/rabbitmq/volume/5672/log:/var/log/rabbitmq -v /xzlawin/mydocker/rabbitmq/volume/5672/data:/var/lib/rabbitmq -v /xzlawin/mydocker/rabbitmq/volume/5672/config:/etc/rabbitmq -e RABBITMQ_DEFAULT_USER=guest -e RABBITMQ_DEFAULT_PASS=guest 6c3c2a225947
  • 1.
解决步骤二,查询日志,分析启动失败原因
  • 查询日志,发现log目录没有权限
docker logs -t --tail 50 [容器ID]
  • 1.
failed to open log file at '/var/log/rabbitmq/rabbit@ab5217e3f168_upgrade.log', reason: permission denied
  • 1.
  • 查看原数据卷目录是777权限
ll /var/lib/docker/volumes/rabbitmq_log/
  • 1.

显示

drwxrwxrwx 2 polkitd input 123 Jul 14 02:17 _data
  • 1.
  • 自定义日志数据卷目录,赋予777权限
chmod 777 /xzlawin/mydocker/rabbitmq/volume/5672/log
  • 1.
  • 删除容器
docker stop [容器ID]
docker ps -a [容器ID]
docker rm -f [容器ID]
  • 1.
  • 2.
  • 3.
解决步骤三,指定自定义数据卷位置,启动成功
docker run -d -p 5672:5672 -p 15672:15672 --name rabbitmq -v /xzlawin/mydocker/rabbitmq/volume/5672/plugins:/opt/rabbitmq/plugins -v /xzlawin/mydocker/rabbitmq/volume/5672/log:/var/log/rabbitmq -v /xzlawin/mydocker/rabbitmq/volume/5672/data:/var/lib/rabbitmq -v /xzlawin/mydocker/rabbitmq/volume/5672/config:/etc/rabbitmq -e RABBITMQ_DEFAULT_USER=guest -e RABBITMQ_DEFAULT_PASS=guest 6c3c2a225947
  • 1.