This sample demonstrates how to use Docker Linux containers to host NServiceBus endpoints communicating over the RabbitMQ transport.
This sample requires that the following tools are installed:
- .NET Core 3.1 SDK
- Docker Community Edition or higher
- If using Windows, configure Docker to use Linux containers to support the Linux-based RabbitMQ container
Running the sample involves building the container images and starting the multi-container application.
Building the container images using the following command will
dotnet publish (which includes
dotnet restore and
dotnet build) the endpoints in addition to building the container images for both the
Sender and the
$ docker-compose build
When the container images are ready, the containers can be started:
$ docker-compose up -d
Both containers log to the console. These logs can be inspected:
$ docker-compose logs sender $ docker-compose logs receiver
The containers can be stopped and removed:
$ docker-compose down
This sample consists of
Receiver endpoints exchanging messages using the RabbitMQ transport. Each of these three components runs in a separate Docker Linux container.
Each endpoint is a container built on top of the official
mcr. image from Docker Hub. The container image builds and publishes the endpoint binaries and then uses those artifacts to build the final container image:
FROM mcr.microsoft.com/dotnet/core/runtime:3.1 AS base FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build WORKDIR /src COPY . . WORKDIR /src/Receiver RUN dotnet publish -c Release -o /app FROM base AS final WORKDIR /app COPY --from=build /app . ENTRYPOINT ["dotnet", "Receiver.dll"]
version: "3.8" services: sender: image: sender build: context: . dockerfile: ./Sender/Dockerfile depends_on: - rabbitmq receiver: image: receiver build: context: . dockerfile: ./Receiver/Dockerfile depends_on: - rabbitmq rabbitmq: image: "rabbitmq:3.8-management" ports: - "15672:15672" - "5672:5672" healthcheck: test: ["CMD-SHELL", "if rabbitmqctl status; then \nexit 0 \nfi \nexit 1"] interval: 10s retries: 5
Endpoints configure the RabbitMQ transport to use the broker instance running in the
var transport = endpointConfiguration.UseTransport(new RabbitMQTransport(Topology.Conventional, "host=rabbitmq"));
Both endpoints block startup until the broker becomes available using the shared
ProceedIfRabbitMqIsAlive hosted service.