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.
Windows Container support will be coming in a future release
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
- 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 (.NET Core).
- Give your new application a name (or take the default) and select OK.
- Under ASP.NET Core 1.1 Templates, select Web Application and select OK.
2. Add Docker support
In the Visual Studio Solution Explorer, right-click the project and select Add > Docker Support from the context menu.
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.
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.
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 will launch 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!
4. 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 About() method with the following:
string message = "Your application description page from within a Container"; ViewData["Message"] = message;
- Set a breakpoint to the left of the
string message... 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 Visual Studio 2015 Tools for Docker, 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
- Docker Tools for Visual Studio - Developing your .NET Core code in a container
- Docker Tools for Visual Studio Team Services - 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 Container Service - Azure Container Service Content
- For more examples of working with Docker, see Working with Docker from the HealthClinic.biz 2015 Connect demo. For more quickstarts from the HealthClinic.biz demo, see Azure Developer Tools Quickstarts.