Prerequisites
To follow this tutorial, you will need the following:- Visual Studio 2022 with ASP.NET and web development workload.
- .NET Framework 4.7.2 or later installed.
- A basic knowledge of C# and ASP.NET Web API.
- Docker Desktop configured to use Windows containers.
This guide assumes that you have a basic knowledge of the above-mentioned technologies and tools. If you are not
familiar with any of them, it is highly recommended to review their official documentation and tutorials to get up to
speed before proceeding with this guide.
Scaffolding the .NET Framework app
Create a new ASP.NET Web API project using Visual Studio:- Open Visual Studio 2022
- Click “Create a new project”
- Select “ASP.NET Web Application (.NET Framework)”
- Name the project
DotNetApiand click “Create” - Select “Web API” template and ensure ”.NET Framework 4.7.2” is selected
- Click “Create”
Hello World! example
OpenControllers/ValuesController.cs and replace its contents with:
Controllers/ValuesController.cs
/api/values will return the Hello World JSON response.
Create the Docker image
Dockerizing makes the app run anywhere that supports Windows containers. This guide uses Windows Server 2022 as the base image.Create .dockerignore
Before creating the Dockerfile, let’s create a.dockerignore file and add the contents that should not be copied over
to the Docker file system.
.dockerignore
Creating a
.dockerignore file and excluding folders like bin/, obj/, and packages/ is important since these
contain build artifacts from your local machine. The build will happen inside the Docker container with the correct
platform settings.Create Dockerfile
Now, create a Dockerfile. The Dockerfile is a text file that contains the instructions for Docker to build the image. The Dockerfile uses Multi-Stage Builds which is the recommended approach for .NET Framework applications. This produces a smaller final image by separating the build environment from the runtime environment.Dockerfile
The
mcr.microsoft.com/dotnet/framework/aspnet:4.8-windowsservercore-ltsc2022 image includes IIS and ASP.NET 4.8,
which is fully backward compatible with applications targeting .NET Framework 4.7.x.Build Docker image
Now you can build the Docker image. To build the Docker image:dotnet-api image using Windows containers.
Windows containers do not support cross-platform builds like Linux containers. You must build Windows container images
on a Windows host with Docker configured to use Windows containers.
Run the Docker image
Let’s run the Docker container using the image created of the .NET Framework application with the command below.-d: Runs the container in detached (background) mode.--name dotnet-api: Name of the containerdotnet-api.-p 8080:80: Maps port 8080 on your host to port 80 in the container (IIS default port).dotnet-api:latestat the end is the name and tag of the image.
http://localhost:8080/api/values to see the .NET Framework application running inside
the Docker container.
To view logs from the container: