Implementation And Containerization Of Microservices Using .NET Core 6 And Docker

Introduction

Here, I’ll give you an example of implementing and containerizing microservices using .NET Core 6 and Docker. In this example, we’ll create two simple microservices, a “ProductService” and an “OrderService,” and then containerize them using Docker.

Step 1: Create Microservices

Create two separate .NET Core 6 Web API projects for the “ProductService” and “OrderService.”

Step 2: Implement Microservices

For simplicity, let’s create a basic CRUD API in each microservice. Here’s how the controllers might look:

ProductService Controller (ProductController.cs):

using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;

namespace ProductService.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class ProductController : ControllerBase
    {
        private static readonly List<Product> _products = new List<Product>();

        [HttpGet]
        public IActionResult GetAllProducts()
        {
            return Ok(_products);
        }

        [HttpPost]
        public IActionResult AddProduct([FromBody] Product product)
        {
            _products.Add(product);
            return Ok("Product added successfully.");
        }
    }

    public class Product
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public decimal Price { get; set; }
    }
}

OrderService Controller (OrderController.cs):

using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;

namespace OrderService.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class OrderController : ControllerBase
    {
        private static readonly List<Order> _orders = new List<Order>();

        [HttpGet]
        public IActionResult GetAllOrders()
        {
            return Ok(_orders);
        }

        [HttpPost]
        public IActionResult PlaceOrder([FromBody] Order order)
        {
            _orders.Add(order);
            return Ok("Order placed successfully.");
        }
    }

    public class Order
    {
        public int Id { get; set; }
        public int ProductId { get; set; }
        public int Quantity { get; set; }
    }
}

Step 3: Create Dockerfiles

Create a Dockerfile for each microservice to define the Docker image:

ProductService Dockerfile (Dockerfile in the ProductService project folder):

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["ProductService/ProductService.csproj", "ProductService/"]
RUN dotnet restore "ProductService/ProductService.csproj"
COPY . .
WORKDIR "/src/ProductService"
RUN dotnet build "ProductService.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "ProductService.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "ProductService.dll"]

OrderService Dockerfile (Dockerfile in OrderService project folder):

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["OrderService/OrderService.csproj", "OrderService/"]
RUN dotnet restore "OrderService/OrderService.csproj"
COPY . .
WORKDIR "/src/OrderService"
RUN dotnet build "OrderService.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "OrderService.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "OrderService.dll"]

Step 4: Build Docker Images

Open a terminal and navigate to each microservice’s project folder and build the Docker image using the following commands:

For ProductService:

docker build -t productservice .

For OrderService:

docker build -t orderservice .

Step 5: Run Docker Containers

Run Docker containers for each microservice using the following commands:

For ProductService:

docker run -d --name productservice -p 8001:80 productservice

For OrderService:

docker run -d --name orderservice -p 8002:80 orderservice

Step 6: Test Microservices

Use a tool like Postman to test the microservices by sending requests to the following endpoints:

ProductService:

  • GET http://localhost:8001/api/product (Get all products)
  • POST http://localhost:8001/api/product (Add a product)

OrderService:

  • GET http://localhost:8002/api/order (Get all orders)
  • POST http://localhost:8002/api/order (Place an order)

This example demonstrates how to create, containerize, and run two simple microservices using .NET Core 6 and Docker. In a real-world scenario, you would handle more complex logic, use a database for data storage, implement error handling, and potentially use an orchestration tool like Docker Com

Leave a Reply

Your email address will not be published. Required fields are marked *