I am trying to run docker-compose with two .net core console applications having dependency of rabbitMQ, I am using Docker with Windows.
I have added to console application, as described on RabbitMQ official docs https://www.rabbitmq.com/tutorials/tutorial-one-dotnet.html
Outside of both applications I have added docker-compose.yml
Folder structure:
Dockerfile
FROM mcr.microsoft.com/dotnet/core/sdk:2.2 as build-env
WORKDIR /app
# Copy the project file and restore the dependencies
COPY *.csproj ./
RUN dotnet restore
# Copy the remaining source files and build the application
COPY . ./
RUN dotnet publish -c Release -o out
# Build the runtime image
FROM mcr.microsoft.com/dotnet/core/sdk:2.2
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "Send.dll"]
Recieve.cs (as it is in RabbitMQ docs)
Recieve.csproj
Dockerfile (Same as Sender Dockerfile except Send replaced with Recieve)
version: '3'
services:
rabbitmq:
image: rabbitmq:3-management
ports:
- "5672:5672"
- "15672:15672"
container_name: rabbitmq
hostname: my-rabbit
Send:
image: sender
build:
context: ./Send
dockerfile: Dockerfile
depends_on:
- rabbitmq
Reciever:
image: reciever
build:
context: ./Recieve
dockerfile: Dockerfile
depends_on:
- rabbitmq
Error Thrown
Unhandled Exception: RabbitMQ.Client.Exceptions.BrokerUnreachableException: None of the specified endpoints were reachable ---> System.AggregateException: One or more errors occurred. (Connection failed) ---> RabbitMQ.Client.Exceptions.ConnectFailureException: Connection failed ---> System.Net.Internals.SocketExceptionFactory+ExtendedSocketException: Connection refused 127.0.0.1:5672
Victor's answer was also helpful but, problem was the rabbitmq starting later, while send and receiver started before.
While I restarted the same stopped sender and receiver container, it was working as expected(because the rabbitmq container was up). So I have added the retry policies on container, found from here.
The final version of my docker-compose file is looking like below, and is working fine. version: '3' services:
rabbitmq:
image: rabbitmq:3-management
ports:
- "5672:5672"
- "15672:15672"
container_name: rabbitmq
hostname: my-rabbit
networks:
- my-network-name
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:15672"]
interval: 30s
timeout: 10s
retries: 5
Send:
image: sender
build:
context: ./Send
dockerfile: Dockerfile
depends_on:
- rabbitmq
restart: on-failure
networks:
- my-network-name
links:
- rabbitmq
Reciever:
image: reciever
build:
context: ./Recieve
dockerfile: Dockerfile
depends_on:
- rabbitmq
restart: on-failure
networks:
- my-network-name
links:
- rabbitmq
networks:
my-network-name:
driver: bridge