Reputation: 13
I'm trying to connect an Express app to RabbitMQ in a docker container. The OS I'm using is Windows 10 and only RabbitMQ is dockerized.
This is the docker-compose file:
version: '3'
services:
rabbitmq:
image: rabbitmq:3-management
hostname: rabbitmq
ports:
- 5672:5672
- 15672:15672
expose:
- 5672
- 15672
The code for create connection to RabbitMQ:
// step 1: Connect to the rabbitmq server
// step 2: Create a new channel on that connection
// step 3: Create the exchange
// step 4: Publish the message to the exchange with a routing key
const amqp = require("amqplib");
class Producer {
channel;
async createChannel() {
const connection = await amqp.connect('amqp://localhost:5672');
this.channel = await connection.createChannel();
}
async publishMessage(routingKey, message) {
if (!this.channel) {
this.createChannel();
}
const exchangeName = "logExchange";
await this.channel.assertExchange(exchangeName, "direct");
const logDetail = {
logType: routingKey,
message: message,
dateTime: new Date(),
};
await this.channel.publish(
exchangeName,
routingKey,
Buffer.from(JSON.stringify({logDetail}))
);
console.log(`The message ${message} is sent to exchange`);
}
}
module.exports = Producer;
The arguments passed in to amqp.connect() I have used: amqp://localhost:5672, amqp://localhost:15672, amqp://rabbitmq:5672, amqp://rabbitmq:15672, amqp://guest:guest@localhost:5672, amqp://guest:guest@localhost:15672, amqp://guest:guest@rabbitmq:5672, amqp://guest:guest@rabbitmq:15672, amqp://rabbitmq, amqp://localhost and still there is no connection to RabbitMQ. I also replaced localhost or rabbitmq with 0.0.0.0 since in the docker desktop show that the ports are map to 0.0.0.0:5672 and 0.0.0.0:15672 in my laptop as in the image: The log of the container doesn't show anything when I start the app, it only show log when I go in to the dashboard using the browser.
Can anyone tell me what is the problem, is this in my code, the OS or problem with docker and the network? Some of the stackoverflow answer I have tried: Can't connect RabbitMQ to my app from docker Can not connect to rabbitmq server in docker with error:Connection refused consumer: Cannot connect to amqp://user:**@localhost:5672//: [Errno 111] Connection refused
I hope someone knows what is wrong because I can't find the solution by browsing internet.
Upvotes: 0
Views: 819
Reputation: 697
The environment in your docker container is not the same shared with your host. You need to check what is the ip attached to your container in order to access it or configure your network correctly.
If you want to speed up your test with rbmq. You can use cloudAMQP for a free rabbitMQ worker.
Upvotes: 0