Muhammad Imran Khan
Muhammad Imran Khan

Reputation: 325

RabbitMq - Docker-compose - How to fix 'Connection refused 127.0.0.1:5672' exception

I have tried several possible solutions on Stack Overflow but nothing seems to work for me.

I am developing microservices using RabbitMQ. The solution contains multiple projects and runs without any problem, but as soon as I use docker-compose option to build the project, Visual Studio throws the following exception:

RabbitMQ.Client.Exceptions.BrokerUnreachableException ExtendedSocketException: Connection refused 127.0.0.1:5672

In my solution, I have three projects communicating with each other via RabbitMQ.

Below is the code for my YAML file.

My docker-compose.yaml:

version: '3.4'

services:    
  rabbitmq:
    hostname: webnet
    image: rabbitmq:3.7.2-management
    ports:
      - "15672:15672"
      - "5672:5672"    
    networks:
      - webnet

  sql-server-db:
    container_name: sql-server-db
    image: microsoft/mssql-server-linux:2017-latest
    ports:
      - "1433:1433"
    environment:
      SA_PASSWORD: "customerdbalten@123"
      ACCEPT_EULA: "Y"
    networks:
      - webnet

  myproject.simulation.api:
    image: ${DOCKER_REGISTRY-}myprojectsimulationapi
    build:
      context: .
      dockerfile: myproject.Simulation.Api/Dockerfile
    links:
      - rabbitmq
    ports:
      - '5000'
    networks:
      - webnet

  myproject.updateservice.api:
    image: ${DOCKER_REGISTRY-}myprojectupdateserviceapi
    build:
      context: .
      dockerfile: myproject.updateservice.Api/Dockerfile
    links:
      - rabbitmq
      - sql-server-db
    ports:
      - '5050'
    networks:
      - webnet

  myproject.web:
    image: ${DOCKER_REGISTRY-}myprojectweb
    build:
      context: .
      dockerfile: MyProject.Web/Dockerfile
    links:
      - rabbitmq
    ports:
      - '5001'
    networks:
      - webnet

networks:
  webnet:
    driver: bridge

My Docker file:

My Dockerfile looks like the following:

FROM microsoft/dotnet:2.2-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM microsoft/dotnet:2.2-sdk AS build
WORKDIR /src
COPY 
MyProject.UpdateService.Api/MyProject.UpdateService.Api.csproj MyProject.UpdateService.Api/
COPY MyProject.Common/MyProject.Common.csproj MyProject.Common/
RUN dotnet restore MyProject.UpdateService.Api/MyProject.UpdateService.Api.csproj
COPY . .
WORKDIR /src/MyProject.UpdateService.Api
RUN dotnet build MyProject.UpdateService.Api.csproj -c Release -o /app

FROM build AS publish
RUN dotnet publish MyProject.UpdateService.Api.csproj -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "MyProject.UpdateService.Api.dll"]

I've also created another simple solution with nothing but two projects - a sender and receiver - that uses RabbitMQ. This solution throws the same exception while docker-composeing, otherwise, it just runs. The YAML file has nothing but auto-generated code.

Upvotes: 4

Views: 4330

Answers (1)

Adiii
Adiii

Reputation: 59946

From the discussion, we found out that the rabbitMQ container is not running as there is already running rabbitMQ service on the host.

You have two option, stop the host RabbitMQ service and then try to connect with rabbitMQ container.

Hostname: rabbitmq:5672

Or if you want to connect with Host RabbitMQ service then you can use

Hostname: host.docker.internal
#or
Hostname: HOST_IP

I WANT TO CONNECT FROM A CONTAINER TO A SERVICE ON THE HOST

The host has a changing IP address (or none if you have no network access). From 18.03 onwards our recommendation is to connect to the special DNS name `host.docker.internal, which resolves to the internal IP address used by the host. This is for development purpose and will not work in a production environment outside of Docker Desktop for Windows.

The gateway is also reachable as gateway.docker.internal.

docker-for-windows-networking

Upvotes: 6

Related Questions