Wednesday, February 9, 2022

Docker: Containerizing a Visual Studio 2022 .NET Core Console Application with Docker

When a .NET 6, ASP.NET Core project is created by Visual Studio 2022 an "Enable Docker" checkbox is provided as part of the project creation wizard. Simply checking the "Enable Docker" checkbox adds Docker support to the ASP.NET core web application.

Other Visual Studio 2022 project types including .NET Core console projects are not afforded the option to enable Docker support during project creation. This post demonstrates how to add Docker support to a Windows .NET Core console application support created with Visual Studio 2022. Additionally this post will drill down into the changes made the project and solution when Docker support is added.

Microsoft does provide an excellent tutorial on how to create a .NET Core console application using Visual Studio Code, Tutorial: Containerize a .NET app. The aforementioned tutorial shows there are significantly more manual steps to Dockerize a console project using Visual Studio Code compared to Visual Studio 2022 (just a few clicks).

The steps to add Docker support to a console application are as follows:

1) Create a .NET Core console application (see: Docker: Create a .NET Core (.NET 6) Console Application in Visual Studio 2022 for future Containerization)

2) Setup the console application to be published (see: Docker: Publishing a .NET Core (.NET 6) Console Application with Visual Studio 2022 (a Containerization Prereq)). Technically a Docker Support can be added without configuring publish for the project but it is cleaner to configure publishing for the project.

3) Right click on the .NET Core console project in Solution Explorer (project ForensicDataPusher) and select Add | Docker Support:

4) Once Add | Docker Support is selected the Docker File Options dialog is displayed:

5) Chose the Target OS. For this example Windows will be selected.

Changes Made by invoking "Docker support"

The following write up from Microsoft describes the changes selecting "Docker support" makes to the project Visual Studio Container Tools for Docker (to quote):

The Dockerfile created by clicking on "Docker support" is as follows:

The .dockerignore file is created at the solution level. The .dockerignore file is not visible in Solution Explorer unless it is explicitly added (right click on the solution in Solution Explorer and select Add | Existing Item). The contents of the .dockerignore file created are as follows:

Visual Studio 2022's standard toolbar looks as follows before Docker support was added:

Visual Studio 2022's standard toolbar looks as follows after Docker support is added:

The startup project command changed from ForensicDataPusher (build the solution) to Docker (deploy to Docker).

The ForensicDataPusher .csproj is changed as follows (see the line demarcated in boldface)

Microsoft documents the DockerDefaultTargetOS project element in Container Tools build properties as follows:

No comments :

Post a Comment