0

I am starting a container for rabbitmq and another with my spring boot service using the following docker-compose.yaml

version: '3'
services:
  messaging:
    image: "messaging"
    ports: 
     - "7878:9876"
    depends_on:
     - rmq
  rmq:
    image: "rabbitmq:management"
    hostname: "rabbitmqhost"
    container_name: "rabbit-mq"
    ports: 
     - "15672:15672"
     - "5672:5672"

Now rabbitmq management is accessible at http://localhost:15672/ and the service is accessible at http://localhost:7878

The service is supposed to push messages into rabbitmq. When I hit the restAPI http://localhost:7878/sendPersonDetails it is unable to connect to rabbitmq.

Following is the error response I am getting-

error occurred in message handler [org.springframework.integration.amqp.outbound.AmqpOutboundEndpoint@3e66d8c7]; nested exception is org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused (Connection refused)

Following is my application.properties file-

spring.cloud.stream.bindings.output.destination=person
spring.cloud.stream.bindings.output.content-type=application/json

server.port=9876

How do I change my docker-compose configuration or service configuration to make it work? Any help's appreciated.

sk17261205
  • 421
  • 1
  • 5
  • 12

2 Answers2

2

Thanks to docker-compose, you can access rmq container by the service name (http://rabbit-mq:port) from the other container. No need to expose ports either.

By default Compose sets up a single network for your app. Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by them at a hostname identical to the container name.

https://docs.docker.com/compose/networking/

Nordle
  • 2,915
  • 3
  • 16
  • 34
Zak
  • 1,005
  • 10
  • 22
1

The issue is that localhost inside the rabbitmq container is not the same localhost you see from your host machine. Indeed every container has a localhost address which point to its IP.

The way to connect container together is to use docker networks. See this other answer for more details: Connecting two docker containers

nivox
  • 2,060
  • 17
  • 18