Skip to main content

Development

Docker Bootcamp – Debug Windows Containers

Web API Using Azure

Welcome back to docker bootcamp.  In the previous post, we learned how to switch from Linux containers to Windows containers.  In this post we will create a new .net project and deploy to a container.  We will also see how to attach the debugger to the code running inside the container.  The IIS image we use for this example does not have .net framework installed by default.  We’ll install it once our container is up and running.  If you were building your own image, you could install it as part of the image.

Create a Visual Studio Project

  • Open Visual Studio 2019
  • Create a new project
    • Use project template asp.net web application (.net framework)
      • You can use VB or C#
    • Configure your project
      • Choose your project name and location
      • Select .net framework 4.7.2
    • Create a new application
      • Choose MVC application
      • Uncheck the box to configure https
      • Uncheck the box for docker support
    • Click create button
  • View the site with iis express
    • Click start debugging from the debug menu
      • Validate the sample site loads correctly
  • Create publish settings
    • Right click on project -> Publish
    • On the publish target window, choose folder
    • Set the folder location to “build”
      • This path is relative to your solution
    • Click finish
  • Publish the solution
    • Make sure you publish using debug configuration
  • Change permissions on the publish target folder
    • In file explorer, right click the build folder -> Properties
    • On the security tab, click edit to change permissions
    • Add the “Everyone” user
    • Give the Everyone user read & execute, list folder contents, and read permissions
    • Click apply

Create a Windows Container

  • Create and start a detached container, attach a volume, and map exposed port to a specified host port
    • docker run -d -v <path to folder>\build:c:\inetpub\wwwroot -p 8080:80 –name debugtest mcr.microsoft.com/windows/servercore/iis
  • View the site
    • Browse to localhost:8080
      • Notice the default iis site loads

Install .NET Framework

  • Execute a command to start an interactive powershell terminal
    • docker exec -it defaultsite powershell.exe
  • Enable .NET Framework for IIS
    • dism /online /enable-feature /featurename:IIS-ASPNET45 /all
  • View the site
    • Browse to localhost:8080
      • Validate the sample site loads correctly

View Containers in Visual Studio

Similar to docker desktop or the docker inspect command, this window lets you see many details about your containers.  You can see status, environment variables, port mappings, logs, and browse the file system of the container.

  • Open the containers window
    • View -> other windows -> containers

Prepare for debugging

  • Open the file Controllers/HomeController.cs
  • Add breakpoints on the return statement of the Index and About methods

Debug Containers in Visual Studio

  • Option 1 – From the containers window
    • Select the container
    • Click the attach to process icon
      • The connection type and connection target fields are automatically selected
  • Option 2 – From the Debug menu -> Attach to Process
    • Select the connection type Docker Windows Container
    • Click find next to connection target
      • This will list the available docker containers on your machine
      • Select debugtest from the list
  • Click select next to attach to
    • Select Managed .NET 4x under debug these code types
      • If your breakpoints do not load, try choosing a different option here
  • Under the list of available processes check the box for show process from all users
  • In the list of available processes, choose w3wp.exe
  • Click attach
  • View the site
    • Browse to localhost:8080
      • Notice the breakpoints are hit

We now have a full windows container development environment.  Our source code is stored on our host machine and deployed to a container through a volume.  We can attach the debugger to the container and debug as if the code were running on the host machine.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Eric Sanner, Solutions Architect

More from this Author

Categories
Follow Us