There are many reasons why you might want to dockerize an ASP.NET Core application. But ultimately, it’s simply going to make life much easier for you. It’s great for isolating components, especially if you’re building a microservices or planning to deploy your application on the cloud.
So, if you want an easier life (possibly) follow this tutorial to learn how to dockerize an ASP.NET Core application.
Get started: Dockerize an ASP.NET Core application
- Create a new ASP.NET Core Web Application in Visual Studio 2017 and click OK:
- On the next screen, select Web Application (Model-View-Controller) or any type you like, while ensuring that ASP.NET Core 2.0 is selected from the drop-down list. Then check the Enable Docker Support checkbox. This will enable the OS drop-down list. Select Windows here and then click on the OK button:
If you right-click on the Docker icon in the taskbar, you will see that you have an option to enable Windows containers there too. You can switch to Windows containers from the Docker icon in the taskbar by clicking on the Switch to Windows containers option:
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 DockerApp/DockerApp.csproj DockerApp/ RUN dotnet restore COPY . . WORKDIR /src/DockerApp 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", "DockerApp.dll"]
When you have a look at the menu in Visual Studio 2017, you will notice that the Run button has been changed to Docker:
- Clicking on the Docker button to debug your ASP.NET Core application, you will notice that there are a few things popping up in the Output window. Of particular interest is the IP address at the end. In my case, it reads Launching http://172.24.12.112 (yours will differ):
This is great and really easy to get started with. But what do you need to do to Dockerize an ASP.NET Core application that already exists? As it turns out, this isn’t as difficult as you may think.
How to add Docker support to an existing .NET Core application
Imagine that you have an ASP.NET Core application without Docker support. To add Docker support to this existing application, simply add it from the context menu:
To add Docker support to an existing ASP.NET Core application, you need to do the following:
- Right-click on your project in Solution Explorer
- Click on the Add menu item
- Click on Docker Support in the fly-out menu:
- Visual Studio 2017 now asks you what the target OS is going to be. In our case, we are going to target Windows:
- After clicking on the OK button, Visual Studio 2017 will begin to add the Docker support to your project:
It’s actually extremely easy to create ASP.NET Core applications that have Docker support baked in, and even easier to add Docker support to existing ASP.NET Core applications.
Lastly, if you experience any issues, such as file access issues, ensure that your antivirus software has excluded your Dockerfile from scanning. Also, make sure that you run Visual Studio as Administrator.
This tutorial has been taken from C# 7 and .NET Core Blueprints.