Reputation: 2178
I have a sample Spring Boot app that produce message:
@Scheduled(fixedRate = 10000)
public void test() throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
try (Connection connection = factory.newConnection();
Channel channel = connection.createChannel()) {
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
String message = "Hello Rabbit!";
channel.basicPublish("", QUEUE_NAME, null, message.getBytes(StandardCharsets.UTF_8));
System.out.println(" [x] Sent '" + message + "'");
}
}
It is dockerized:
FROM openjdk:11 as jdkbase
FROM jdkbase
COPY target/RabbitMQtester-0.0.1-SNAPSHOT.jar /run/RabbitMQtester-0.0.1-SNAPSHOT.jar
#ADD target/sfida-1.0.0.jar /run/sfida-1.0.0.jar
ENTRYPOINT java -Dfile.encoding=UTF-8 -jar /run/RabbitMQtester-0.0.1-SNAPSHOT.jar
I need to build an image containing both RabbitMQ and my app. Here is docker-compose.yml for this task:
version: "3.7"
services:
mytestapp:
build: .
rabbitmq:
image: rabbitmq:3.8.3-management-alpine
hostname: localhost
environment:
RABBITMQ_DEFAULT_USER: guest
RABBITMQ_DEFAULT_PASS: guest
ports:
- 5672:5672
- 15672:15672
When i start the resulting image, i can access rabbitMQ admin page from my host. Also, i can run my app from the host and it'll access rabbitMQ. But my app, which is in the container/image, can't access RabbitMQ (it throws "Connection refused (Connection refused)" errors) What is wrong with my configurations?
UPD: I've also tried to change docker-compose.yml to this (and connection in my app to factory.setHost("rabbitmq");
)
version: "3.8"
services:
mytestapp:
build: .
networks:
- some-net
rabbitmq:
image: rabbitmq:3.8.3-management-alpine
hostname: rabbitmq
networks:
- some-net
environment:
RABBITMQ_DEFAULT_USER: guest
RABBITMQ_DEFAULT_PASS: guest
ports:
- 5672:5672
- 15672:15672
networks:
some-net:
driver: bridge
and to this:
version: "3.8"
services:
mytestapp:
build: .
rabbitmq:
image: rabbitmq:3.8.3-management-alpine
hostname: rabbitmq
environment:
RABBITMQ_DEFAULT_USER: guest
RABBITMQ_DEFAULT_PASS: guest
ports:
- 5672:5672
- 15672:15672
but result is the same...
Upvotes: 2
Views: 542
Reputation: 76
The RabbitMQ service is by default accessible using the name of the docker service rabbitmq
(when you remove hostname: localhost
) from other services in the same stack (provided they are configured with the same network, in this case the default). So you should replace factory.setHost("localhost");
with factory.setHost("rabbitmq");
and remove hostname: localhost
.
You did specify hostname: localhost
, so one could assume RabbitMQ can be accessed using localhost
hostname from the mytestapp
service, but since it's not working it's very likely conflicting with the default loopback and resolving to 127.0.0.1
instead.
Upvotes: 1