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.
127.0.0.1:5672, it should berabbitmq:5672. keep in mindlocalhostin contain mean this container, not rabbitmq container. better to show your connections tring. - Adiiidocker-composeif want to connect with rabbitmq it should be likerabbitmq:5672, remember in service to service communication you must use service name or container name to connect. - Adiiirabbitmq:5672instead of the127.0.0.1:5672. Yes these pls updateappsettings.json file: Hostname: rabbitmq:5672- Adiii