Docker Compose Setup

The docker-compose.yml file orchestrates the setup for a development environment including service definitions for Zipkin, RabbitMQ, and two example applications: a web API and a worker service.

version: '3.8'

services:
  zipkin:
    image: openzipkin/zipkin
    ports:
      - 9411:9411

  rabbitmq:
    image: rabbitmq:3-management-alpine
    ports:
      - 5672:5672
      - 15672:15672

  webapi:
    build:
      context: ../..
      dockerfile: ./examples/MicroserviceExample/WebApi/Dockerfile
    image: opentelemetry-example-webapi
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - RABBITMQ_HOSTNAME=rabbitmq
      - RABBITMQ_DEFAULT_USER=guest
      - RABBITMQ_DEFAULT_PASS=guest
      - ZIPKIN_HOSTNAME=zipkin
    ports:
      - 5000:5000
    depends_on:
      - rabbitmq
      - zipkin

  workerservice:
    build:
      context: ../..
      dockerfile: ./examples/MicroserviceExample/WorkerService/Dockerfile
    image: opentelemetry-example-workerservice
    environment:
      - DOTNET_ENVIRONMENT=Development
      - RABBITMQ_HOSTNAME=rabbitmq
      - RABBITMQ_DEFAULT_USER=guest
      - RABBITMQ_DEFAULT_PASS=guest
      - ZIPKIN_HOSTNAME=zipkin
    restart: on-failure
    depends_on:
      - rabbitmq
      - zipkin

Service Descriptions

  • Zipkin: Used for distributed tracing.
  • RabbitMQ: A message broker that allows for asynchronous communication between services.
  • Web API: A sample API that interacts with RabbitMQ and Zipkin for tracing and message handling.
  • Worker Service: A sample worker that processes messages from RabbitMQ and can also send tracing information to Zipkin.

Building and Running the Environment

To build and run the services defined in the docker-compose.yml file, use the following command in the terminal:

docker-compose up --build

This command will build the images defined in the web API and worker service, and pull the necessary images for Zipkin and RabbitMQ.

Dockerfile Structure

Each service specifies a Dockerfile, crucial for the building process. Below is an example of the Dockerfile used for the worker service.

ARG SDK_VERSION=8.0
FROM mcr.microsoft.com/dotnet/sdk:${SDK_VERSION} AS build
ARG PUBLISH_CONFIGURATION=Release
ARG PUBLISH_FRAMEWORK=net8.0
WORKDIR /app
COPY . ./
RUN dotnet publish ./examples/MicroserviceExample/WorkerService -c "${PUBLISH_CONFIGURATION}" -f "${PUBLISH_FRAMEWORK}" -o /out -p:IntegrationBuild=true

FROM mcr.microsoft.com/dotnet/aspnet:${SDK_VERSION} AS runtime
WORKDIR /app
COPY --from=build /out ./
ENTRYPOINT ["dotnet", "WorkerService.dll"]

Dockerfile Breakdown

  1. Base Image: The first part of the Dockerfile uses the .NET SDK image for building the application.
  2. Set Working Directory: The WORKDIR directive sets the working directory to /app.
  3. Copy Source Code: The COPY . ./ command copies the contents from the host’s directory into the container.
  4. Publish Application: The dotnet publish command compiles the application and prepares it for deployment, outputting the result to the /out directory.
  5. Runtime Image: The second part of the Dockerfile utilizes the .NET runtime image for the service, pulling the built application from the previous step.
  6. Entry Point: Sets the entry point to execute the WorkerService.

Environment Variable Configuration

In the development environment, it’s important to configure several environment variables to ensure proper communication between the services. These include:

  • ASPNETCORE_ENVIRONMENT: Set to Development for the web API.
  • DOTNET_ENVIRONMENT: Set to Development for the worker service.
  • RABBITMQ_HOSTNAME, RABBITMQ_DEFAULT_USER, RABBITMQ_DEFAULT_PASS: Specify the RabbitMQ connection details.
  • ZIPKIN_HOSTNAME: The hostname for the Zipkin service for tracing.

These environment variables help the services discover each other at runtime and facilitate proper configuration during execution.

Conclusion

Configuring Docker within the OpenTelemetry .NET development environment allows for a streamlined setup of services crucial for testing and development. By following the outlined steps and utilizing the provided examples, experts can efficiently build and run their applications using Docker.

Sources:

  • Docker Compose file and structure
  • Dockerfile specifications and environment variable setups