CentOS6 Docker容器内RabbitMQ启动后无法连接的问题排查与解决
Alright, let's break down this problem and fix it step by step.
The core issue here is how Docker's RUN command works. Each RUN instruction spins up a temporary, isolated container to execute your command, then stops that container and saves the changes as an image layer.
So when you run RUN service rabbitmq-server start (Step 13), RabbitMQ does start up—but only in that temporary container. As soon as the RUN command finishes, that container shuts down, and the RabbitMQ process dies with it.
Then Step 14's RUN rabbitmqctl status runs in an entirely new, fresh container. There's no RabbitMQ running here at all, hence the nodedown error. The hostname d1837eb4724b is just the hostname of this new temporary container, not the one where you tried to start RabbitMQ earlier.
Here are two solid solutions depending on what you need:
Solution 1: Combine Startup and Status Check in One RUN
If you just want to verify RabbitMQ can start correctly during image build, put both commands in the same RUN so they run in the same container:
RUN service rabbitmq-server start && rabbitmqctl status
This way, the status check runs while RabbitMQ is still up in that temporary container.
Solution 2: Use CMD/ENTRYPOINT to Run RabbitMQ When the Container Starts
This is the proper Docker way to run long-running services. CMD or ENTRYPOINT execute commands when you launch the container (not during build), keeping the container alive as long as the service runs.
Update your Dockerfile like this (keep all your installation steps before this):
RUN chkconfig rabbitmq-server on # Run RabbitMQ in the foreground (keeps the container alive) CMD ["rabbitmq-server"]
Then, after building the image and starting a container with it, you can check the status by exec-ing into the running container:
docker exec -it <your-container-id-or-name> rabbitmqctl status
Bonus: Fix Erlang Node Hostname Issues
If you still run into node connection problems later, you can set a fixed hostname for the RabbitMQ node using an environment variable:
ENV RABBITMQ_NODENAME rabbit@localhost CMD ["rabbitmq-server"]
This ensures RabbitMQ uses a predictable hostname instead of the random Docker container hostname.
内容的提问来源于stack exchange,提问作者Rudziankoŭ

