Debugging apps in a local Docker container

Overview

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.

Prerequisites

The following tools must be 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

  1. From the Visual Studio menu, select File > New > Project.
  2. Under the Templates section of the New Project dialog box, select Visual C# > Web.
  3. Select ASP.NET Core Web Application.
  4. Give your new application a name (or take the default) and select OK.
  5. Select Web Application.
  6. Check the Enable Docker Support checkbox.
  7. 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.

  1. Set the Solution Configuration to Debug and 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.

  2. Go to the About page, which is where we're going to make our changes.

  3. Return to Visual Studio and open Views\Home\About.cshtml.

  4. Add the following HTML content to the end of the file and save the changes.

    <h1>Hello from a Docker Container!</h1>
    
  5. 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
    
  6. Your changes have been applied!

3. Debug with breakpoints

Often, changes will need further inspection, leveraging the debugging features of Visual Studio.

  1. Return to Visual Studio and open About.cshtml.cs

  2. Replace the contents of the OnGet() method with the following:

        Message = "Your application description page from within a Container";
    
  3. Set a breakpoint to the left of the code line.

  4. Hit <F5> to start debugging.

  5. Navigate to the About page to hit your breakpoint.

  6. Switch to Visual Studio to view the breakpoint, and inspect the value of message.

    2

Summary

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.

Troubleshooting

Troubleshooting Visual Studio Docker Development

More about Docker with Visual Studio, Windows, and Azure

Various Docker tools

Some great docker tools (Steve Lasker's blog)

Good articles

Introduction to Microservices from NGINX

Presentations