Running a worker service on Docker

This section is focused on the deployment step of the .NET worker template. We will proceed by running our service on a Docker Linux image. As we have already seen in Chapter 12, The Containerization of Services, we will use Docker to run the application in a container.

Let's start by configuring the Dockerfile in the project folder:

FROM mcr.microsoft.com/dotnet/core/runtime:3.0 AS base
WORKDIR /app

# Step 1 - Building the project
FROM mcr.microsoft.com/dotnet/core/sdk:3.0 AS build
WORKDIR /src
COPY ["HealthCheckWorker.csproj", "./"]
RUN dotnet restore "./HealthCheckWorker.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "HealthCheckWorker.csproj" -c Release -o /app/build

# Step 2 - Publish the project
FROM build AS publish
RUN dotnet publish "HealthCheckWorker.csproj" -c Release -o /app/publish

# Step 3 - Run the project
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "HealthCheckWorker.dll"]

The preceding code describes the container build and deployment process for our .NET Core worker application. The Dockerfile instructions can be grouped into five steps:

  1. The first step executes the build of the project using the dotnet/core/sdk Docker image:
    • First of all, it sets the working directory as /src and copies the files in the project folder.
    • Secondly, it executes the dotnet restore and dotnet build commands.
  1. The second step runs the dotnet publish command using the Release configuration in the /app/publish folder.
  2. The third step uses the dotnet/core/runtime Docker image to run the result of the previously executed dotnet publish using the dotnet CLI command.
  3. It is possible to build the Docker image using the following command:
docker build --rm -f "Dockerfile" -t healthcheckworker:latest 
  1. Furthermore, we can run the previously built image using the following command:
docker run --rm -d healthcheckworker:latest

The preceding command runs the Docker container and, consequently, the worker service process. The worker service will perform an HTTP GET request to the URL configured in the appsettings.json file of the project by applying throttling, also specified in the appsettings.json file.

The previously mentioned Dockerfile uses a multi-stage build approach and some other techniques to build the Docker image used to run the project. These concepts, and more, are described in detail in Chapter 12, The Containerization of Services.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.187.233