Debugging apps in a local Docker container
Visual Studio 2017 provides a consistent way to develop in a Docker container and validate your application locally. You don't have to restart the container each time you make a code change. This article illustrates how to use the "Edit and Refresh" feature to start an ASP.NET Core Web app in a local Docker container, make any necessary changes, and then refresh the browser to see those changes. This article also shows you how to set breakpoints for debugging.
The following tools must be installed.
- Visual Studio 2017 with the Web Development workload installed.
To run Docker containers locally, you'll need a local docker client. You can use the Docker Toolbox, which requires Hyper-V to be disabled, or you can use Docker for Windows, which uses Hyper-V, and requires Windows 10.
If using Docker Toolbox, you'll need to configure the Docker client
1. Create a web app
- From the Visual Studio menu, select File > New > Project.
- Under the Templates section of the New Project dialog box, select Visual C# > Web.
- Select ASP.NET Core Web Application.
- Give your new application a name (or take the default) and select OK.
- Select Web Application.
- Check the Enable Docker Support checkbox.
- Select the type of container you want (Windows or Linux) and click OK.
2. Edit your code and refresh
To quickly iterate changes, you can start your application within a container, and continue to make changes, viewing them as you would with IIS Express.
Set the Solution Configuration to
Debugand press <CTRL + F5> to build your docker image and run it locally.
Once the container image has been built and is running in a Docker container, Visual Studio launches the Web app in your default browser. If you are using the Microsoft Edge browser or otherwise have errors, see Troubleshooting section.
Go to the About page, which is where we're going to make our changes.
Return to Visual Studio and open
Add the following HTML content to the end of the file and save the changes.
<h1>Hello from a Docker Container!</h1>
Viewing the output window, when the .NET build is completed and you see these lines, switch back to your browser and refresh the About page.
Now listening on: http://*:80 Application started. Press Ctrl+C to shut down
Your changes have been applied!
3. Debug with breakpoints
Often, changes will need further inspection, leveraging the debugging features of Visual Studio.
Return to Visual Studio and open
Replace the contents of the OnGet() method with the following:
Message = "Your application description page from within a Container";
Set a breakpoint to the left of the code line.
Hit <F5> to start debugging.
Navigate to the About page to hit your breakpoint.
Switch to Visual Studio to view the breakpoint, and inspect the value of message.
With Docker support in Visual Studio 2017, you can get the productivity of working locally, with the production realism of developing within a Docker container.
More about Docker with Visual Studio, Windows, and Azure
- Container development with Visual Studio - a container development hub page
- Docker Integration for Azure Pipelines - Build and Deploy docker containers
- Docker Tools for Visual Studio Code - Language services for editing docker files, with more e2e scenarios coming
- Windows Container Information- Windows Server and Nano Server information
- Azure Kubernetes Service - Azure Kubernetes Service Documentation