Debugging apps in a local Docker container

Overview

The Visual Studio Tools for Docker provides a consistent way to develop in and validate your application locally in a Linux Docker container. 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.

Note

Windows Container support will be coming in a future release

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 (.NET Core).
  4. Give your new application a name (or take the default) and select OK.
  5. Under ASP.NET Core 1.1 Templates, select Web Application and select OK.

2. Add Docker support

  1. In the Visual Studio Solution Explorer, right-click the project and select Add > Docker Support from the context menu.

    Add Docker Support context menu

  2. Adding Docker support to an ASP.NET Core web project results in the addition of several Docker-related files being added to the project, including Docker-Compose files, deployment Windows PowerShell scripts, and Docker property files.

    Docker files added to project

3. 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 will launch 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!

4. Debug with breakpoints

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

  1. Return to Visual Studio and open Controllers\HomeController.cs
  2. Replace the contents of the About() method with the following:

    string message = "Your application description page from within a Container";
    ViewData["Message"] = message;
    
  3. Set a breakpoint to the left of the string message... 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.

Summary

With Visual Studio 2015 Tools for Docker, 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