Adding Docker support to an existing application

Adding Docker support to an existing application requires a couple of simple steps:

  1. Open the project/solution in Visual Studio 2019 and right-click on the project.
  2. Choose Add and select Docker Support:

Depending on your client tools and Visual Studio configuration, there may also be a Container Orchestrator Support option. With this option, the cloud orchestrator of your choice can be chosen. In this sample, we used Docker because this format is supported by the major container orchestrators. Other cloud orchestrator options do exist, however:

  • Docker Swarm
  • Kubernetes
  • Mesos Marathon

Depending on the cloud orchestrator used, a file is added to the project in the specific format for that orchestrator.

By adding Docker support, a new file is added to the project named Docker. The Dockerfile is the specification of a container image. This file can be read by Docker, which sees it as instructions. The file is a text document that contains separate commands that can also be called within a command-line tool to assemble an image:

FROM mcr.microsoft.com/dotnet/core/aspnet:3.0-buster-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
EXPOSE 555
FROM mcr.microsoft.com/dotnet/core/sdk:3.0-buster AS build
WORKDIR /src
COPY ["ExistingDevOpsProject/ExistingDevOpsProject.csproj",
"ExistingDevOpsProject/"]
RUN dotnet restore "ExistingDevOpsProject/ExistingDevOpsProject.csproj"
COPY . .
WORKDIR "/src/ExistingDevOpsProject"
RUN dotnet build "ExistingDevOpsProject.csproj" -c Release -o
/app/build
FROM build AS publish
RUN dotnet publish "ExistingDevOpsProject.csproj" -c Release -o
/app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "ExistingDevOpsProject.dll"]

The example uses a technique called a multi-stage build file. This is because the file uses multiple FROM statements where there is a reference to a specific image.

Prior to multi-stage build, it wasn't possible to use multiple FROM statements. During this time, it was hard to build efficient container images. Each statement in the file represented an additional layer on the image that resulted in it becoming larger and larger. 

During this build process, it was also necessary to remove any components that were required during this process. For this reason, it was very common to have separate Dockerfiles for development and production.

As mentioned, the Dockerfile comprises instructions and the most commonly used instructions are as follows:

  • FROM: The FROM command is used to specify on which operating system or base image the image will be based. In the example, the mcr.microsoft.com/dotnet/core/aspnet:3.0-buster-slim image is used for the production version of the application, and the mcr.microsoft.com/dotnet/core/sdk:3.0-buster image is used to build the image.
  • RUN: The RUN command is used to install components or perform operations during the build process of the container image.
  • ENTRYPOINT: The ENTRYPOINT command specifies what the entry point for a container image needs to be. In the example, the entry point is specified as a .NET application that references the library that was built during the compilation process.

So far, we've created our application and added Docker support. Next, we'll see how to create an image with the application.

..................Content has been hidden....................

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