Deploying Microservices using Docker

This lesson is part of the course Microservices with C#, .NET Core and Azure . Use navigation on the left.

Let’s start with setting up Docker on the local machine.

  1. Download and install Docker for Windows (https://www.docker.com/products/docker#/windows) to run Docker containers locally.
  2. After installation, enable the Hyper-V feature.
  1. Right-click Docker icon in the system tray, and then click Settings > Shared Drives
  2. Select the local drives that you want to be available to your containers.

We are now all set with docker installation. This should help us enable Docker support when creating our applications using Visual Studio.

  1. Create a web application by enabling Docker support(select windows for OS).
  2. Clicking on create will create a web application with multiple files related to Docker.
  3. The file of importance is Dockerfile. The Dockerfile for ASP.NET Core applications is based on the microsoft/aspnetcore image (https://hub.docker.com/r/microsoft/aspnetcore/). This image includes the ASP.NET Core NuGet packages, which have been prefitted, in order to improve startup performance. When building ASP.NET Core applications, the Dockerfile FROM instruction (command) points to the most recent microsoft/dotnet image (https://hub.docker.com/r/microsoft/dotnet/) on the Docker hub. The following is the default code snippet that’s provided by the template:
FROM mcr.microsoft.com/dotnet/core/aspnet:3.0-nanoserver-1903 
AS base WORKDIR /app 
EXPOSE 80 EXPOSE 443 
FROM mcr.microsoft.com/dotnet/core/aspnet:3.0 AS runtime
WORKDIR /app
COPY ${source:-obj/Docker/publish} .
ENTRYPOINT ["dotnet", "Sportopia.SportsStore.ProductService.dll"]

The code above is basically a set of instructions:

  • FROM is a message that tells Docker to pull the base image onto the existing image, called microsoft/aspnetcore:3.0.
  • COPY and WORKDIR will copy content to a new directory inside the called/app container and set it to the working directory for subsequent instructions.

To expose our Product service on port 80 of the container, we can use EXPOSE ENTRYPOINT, which specifies the command we use to execute when the container starts up. In our example, we have ProductService and our entry point is ["dotnet", "Sportopia.SportsStore.ProductService.dll"].

Now that we have everything setup, all we need to do is to press F5 to run our application. This will launch our app  in the container. Once we have our application/microservice containerized, we can use Azure DevOps and Azure container services to deploy our container to the Azure cloud.

You might be interested in the following courses:

Course Category

Back to: Microservices with C#, .NET Core and Azure > Deployment