ASP.NET Core in a container
In this guide you will learn how to:
- Create a
Dockerfile
file describing a simple .NET Core service container. - Build, run, and verify the functionality of the service.
- Debug the service running as a container.
Prerequisites
- Docker and the VS Code Docker extension must be installed as described on the overview.
- For .NET development, install .NET SDK.
- Microsoft C# for Visual Studio Code extension.
Create a .NET Web API project
-
Create a folder for the project.
-
Open developer command prompt in the project folder and initialize the project:
dotnet new webapi --no-https
Add Docker files to the project
-
Open the project folder in VS Code.
-
Wait for the C# extension to prompt you to add required assets for build and debug, and choose Yes. You can also open the Command Palette (
Ctrl+Shift+P
) and use the .NET: Generate Assets for Build and Debug command. -
Open Command Palette (
Ctrl+Shift+P
) and use Docker: Add Docker Files to Workspace... command: -
Use .NET: ASP.NET Core when prompted for application platform.
-
Choose Windows or Linux when prompted to choose the operating system.
Windows is only applicable if your Docker installation is configured to use Windows containers.
-
You will be asked if you want to add Docker Compose files. We will not use Docker Compose in this tutorial, so both "Yes" and "No" answers are fine.
-
Change the port for application endpoint to
5000
. -
Dockerfile
and.dockerignore
files are added to the workspace.The extension will also create a set of VS Code tasks for building and running the container (in both debug- and release configuration, four tasks in total), and a debugging configuration for launching the container in debug mode.