Getting Started
Architecture
NServiceBus
Transports
Persistence
ServiceInsight
ServicePulse
ServiceControl
Monitoring
Samples

Hosting endpoints in Docker Linux containers

Component: NServiceBus
NuGet Package: NServiceBus (9.x)

This sample demonstrates how to use Docker Linux containers to host NServiceBus endpoints communicating over the RabbitMQ transport. While this sample uses Docker Compose to demonstrate how to orchestrate a multi-container application, the containers are compatible withe other orchestration technologies, for example Kubernetes.

The endpoints use the .NET SDK Container Building Tools to enable the creation of containers via the dotnet publish command. See the Microsoft tutorial and customization documentation for more details.

Prerequisites

To containerize a .NET app using dotnet publish:

Running the sample

Running the sample involves building the container images and starting the multi-container application.

Building container images

Build the container images by using the following commands:

dotnet publish Sender --os linux --arch x64 /t:PublishContainer
dotnet publish Receiver --os linux --arch x64 /t:PublishContainer

Starting containers

When the container images are ready, the containers can be started:

docker-compose up -d

Observing containers

Both containers log to the console. These logs can be inspected:

docker-compose logs sender
docker-compose logs receiver

Stopping and removing containers

The containers can be stopped and removed:

docker-compose down

Code walk-through

This sample consists of Sender and Receiver endpoints exchanging messages using the RabbitMQ transport. Each of these three components runs in a separate Docker Linux container.

Orchestration

Endpoint container images for the Sender and the Receiver are combined with an official RabbitMQ image to create a multi-container application using Docker Compose:

services:
    sender:
        image: sender
        depends_on:
            - rabbitmq
    receiver:
        image: receiver
        depends_on:
            - rabbitmq
    rabbitmq:
        image: "rabbitmq:3.12-management"
        ports:
            - "15672:15672"
            - "5672:5672"
        healthcheck:
            test: ["CMD-SHELL", "if rabbitmqctl status; then \nexit 0 \nfi \nexit 1"]
            interval: 10s
            retries: 5

Transport configuration

Endpoints configure the RabbitMQ transport to use the broker instance running in the rabbitmq container:

var transport = new RabbitMQTransport(RoutingTopology.Conventional(QueueType.Quorum), connectionString);

Waiting for RabbitMQ broker to become available

Both endpoints block startup until the broker becomes available using the shared ProceedIfBrokerIsAlive class.

See the docker documentation for other options to control startup order.

Related Articles

  • Docker Container Host
    Take advantage of process isolation by hosting endpoints in Docker containers.
  • Hosting
    Describes the various approaches to endpoint hosting.