econnrefused 127.0.0.15672 Rabbit-mq with docker compose
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
An ECONNREFUSED error against RabbitMQ in Docker Compose usually means your client tried to connect to the wrong address, the wrong port, or a broker that is not ready yet. The detail that trips people up most often is that 127.0.0.1 means different things depending on whether the code is running on the host machine or inside another container.
Understand the Address First
If your application is running on the host machine, 127.0.0.1:5672 means “connect to the RabbitMQ port published by Docker onto the host.” For that to work, your Compose file must publish the port:
If your application is running inside another Compose service, 127.0.0.1 points back to that application container, not to RabbitMQ. In that case you should use the Compose service name as the host:
Inside the Compose network, rabbitmq is the correct hostname.
Do Not Confuse Port 5672 with 15672
RabbitMQ uses different ports for different purposes:
- '
5672for AMQP client connections' - '
15672for the management web UI'
A common mistake is seeing 15672 in the browser and then accidentally using it for the AMQP client connection. The management UI can be healthy while your application still fails if it tries the wrong port.
Check Whether the Broker Is Actually Running
Before changing application code, confirm that RabbitMQ started cleanly:
If the container is restarting, crashing, or still booting, the connection refusal is expected. A healthy container is necessary, but it still does not guarantee readiness for clients at the exact moment your app starts.
depends_on Does Not Mean Ready
In Compose, depends_on controls startup order, not application readiness. Your app may start before RabbitMQ finishes initializing.
A practical fix is to retry the connection in the app or add a health check:
Then your application can wait for a healthy broker, or at least fail with a clearer startup sequence.
Verify Connectivity from the Right Place
If the app is containerized, test from inside that network rather than from the host:
From there, check whether the broker hostname resolves and the port is reachable. If DNS resolution works for rabbitmq but your code still uses 127.0.0.1, the bug is in configuration, not networking.
Example Client Configuration
A typical Node.js client should use environment variables instead of a hard-coded localhost address:
That makes it easy to use localhost on the host and rabbitmq inside Compose.
Common Pitfalls
The biggest pitfall is using 127.0.0.1 from inside a container. That does not refer to the RabbitMQ container unless RabbitMQ is running inside the same container, which is not the normal Compose setup.
Another common issue is mixing up ports 5672 and 15672. One is for clients, the other is for the web dashboard.
Teams also rely on depends_on and assume it guarantees readiness. It does not. Connection retries or health checks are still needed.
Finally, do not ignore the logs. RabbitMQ startup failures, bad credentials, or plugin problems often show up there immediately.
Summary
- '
ECONNREFUSEDusually means wrong host, wrong port, or a broker that is not ready yet.' - Use
localhostonly when the client runs on the host and the port is published. - Use the Compose service name such as
rabbitmqwhen the client runs in another container. - Connect to port
5672for AMQP and use15672only for the management UI. - Check container logs and add retries or health checks so startup timing does not break the app.
Related reading
- Elasticsearch vs Kafka Putting intelligence in producers
- Embedded Kafka for testing without spring
- Embedded Kafka integration test - consumer never completes
- Embedded Kafka Spring test executes before embedded Kafka is ready
- ECONNREFUSED for Postgres on nodeJS with dockers
- ECS/ECR is common practice to have one repository per image and associated versions?
- ECS Fargate Scheduled Task not running
- ECS unable to assume role

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.