Using Docker with .NET CorePosted by minati biswal on September 9th, 2019 .NET Core is modular and faster when compared to the .NET framework and helps in running applications side by side, where each application is running its own set of CLR libraries and runtime. This makes it perfect for running on Docker containers. The image of .NET Core is far smaller when compared to the image having the .NET framework installed. .NET Core uses a Windows Nano Server or Linux image, which is a lot smaller than the Windows service core image. As .NET Core runs cross-platform, we can also create Docker images of other platforms and run applications on them. With Visual Studio 2017, we can choose Docker while creating a .NET Core or ASP.NET Core project, and it auto scaffolds the Docker files and sets up the basic configuration to run applications on Docker. The following screenshot shows the Docker options available in Visual Studio 2017 to provision Docker containers: Once we create or enable Docker support in our application, it creates the Docker files in our project and also adds another project, named docker-compose, as follows: The docker-compose project contains set of YAML (.yml) files that contain the configuration related to the application hosted in the container and a reference to the path of the Dockerfile created for the project when Docker support was added. Here is the sample docker-compose.yml file that contains two services having details such as the image name, dockerfile path, and so on. This file is from the sample application we discussed previously: version: '1' services: vendor.api: image: vendor.api build: context: . dockerfile: srcmicroservicesVendorVendor.APIDockerfile identity.api: image: identity.api build: context: . dockerfile: srcmicroservicesAuthServerIdentity.AuthServerDockerfile The following is the content of the Dockerfile residing inside the Vendor.API project we created in the sample application above: FROM microsoft/aspnetcore:2.0-nanoserver-1709 AS base WORKDIR /app EXPOSE 80 FROM microsoft/aspnetcore-build:2.0-nanoserver-1709 AS build WORKDIR /src COPY *.sln ./ COPY src/microservices/Vendor/Vendor.API/Vendor.API.csproj src/microservices/Vendor/Vendor.API/ RUN dotnet restore COPY . . WORKDIR /src/src/microservices/Vendor/Vendor.API RUN dotnet build -c Release -o /app FROM build AS publish RUN dotnet publish -c Release -o /app
FROM base AS final WORKDIR /app COPY --from=publish /app . ENTRYPOINT ["dotnet", "Vendor.API.dll"] The preceding Dockerfile starts referencing a base image Microsoft/aspnetcore:2.0-nano server-1709 that will be used to create a Docker container. The COPY command is the actual path where the project files reside. It will then use dotnet CLI commands such as dotnet restore to restore all the NuGet packages inside the container, dotnet build to build the application, and dotnet publish to build and publishes the compiled output into a publish folder inside the container. To get more additional information about docker with dot net to follow .net course course Like it? Share it!More by this author |