Debugging in Docker Container with Visual Studio Code

In the previous posts, we examined:

This time I will show how to develop in VS Code while debugging in a Docker container.

Why?

As I mentioned before, the Docker images produced by Microsoft tend to be updated a little more frequently than the installers for the .NET Core tooling.

By using those container images to build and run the result of your project you assure yourself to use the most recent bits. And, you can run multiple versions of those bits side by side!

And you may want to control the upgrade process for certain projects on which you are working.

The final piece to the puzzle, then, is reaching into the container to perform source code debug.

Fortunately, there is an integration component to help with that.

Prerequisites

In order to make use of remote debugging with ASP.NET Core and Docker you will need the following tools.

Tool Description Installation
yo Tool for running Yeoman generators npm install –global yo
yo aspnet ASP.NET Core generator npm install –global generator-aspnet
yo docker Docker generator npm install –global generator-docker
VS Code Microsoft Visual Studio Code https://code.visualstudio.com/
Docker Docker for Mac¹ https://docs.docker.com/docker-for-mac/

¹ Docker for Windows can also be used, but the instructions below assume Docker for Mac

Preparatory Steps

Install generators
  1. sudo su admin
  2. npm install –global yo
  3. npm install –global generator-aspnet
  4. npm install –global generator-docker
Create a sample app
  1. yo aspnet
    Select Web API app type; Docker.Debug for the name
  2. cd Docker.Debug
  3. code .
  4. Let it add required assets
  5. dotnet restore && dotnet run
  6. Test the result to make sure it is working by going to http://localhost:5000/api/values
  7. yo docker
    Select .NET Core for language and rtm for version; Y for web server and 5000 for the port; choose the defaults for the rest of the questions
Fixup for Newer Versions

The version of the docker generator available at the time I am writing this is based on an older version of the .NET Core runtime and tools. Make the changes below to cause the result to work with the 1.1.0 runtime and tools.

If using Docker for Windows modify the corresponding line in the dockerTasks.ps1 file instead of dockerTasks.sh. Also, you will want to modify the windows task in the launch.js file.

  1. dockerTasks.sh:8 from framework=”netcodeapp1.0″ to framework=”netcoreapp1.1″
  2. Dockerfile.debug:1 from FROM microsoft/dotnet:1.0.0-preview2-sdk to FROM microsoft/dotnet:1.1.0-sdk-projectjson
  3. Dockerfile.debug:5 add line: RUN apt-get update && apt-get install -y unzip
  4. .vscode/launch.js:17 change to “args”: “${auto-detect-url}/api/values”,
  5. Add the following lines back into the top of tasks.json that were removed by yo docker:
        "version": "0.1.0",
        "command": "dotnet",
        "isShellCommand": true,
        "args": [],
        "tasks": [{
            "taskName": "build",
            "args": [
                "${workspaceRoot}/project.json"
            ],
            "isBuildCommand": true,
            "problemMatcher": "$msCompile"
        }],
    
  6. Set breakpoint and hit F5

Caveats

In order to take advantage of various features of VS Code (such as error / warning reporting while typing) you need to have the .NET Core SDK installed locally and in your path.

Typically what I do is manage the .vscode directory in use. I keep .vscode-local and .vscode-docker directories available and copy the correct one to .vscode depending on what I am doing. When I need to debug in a docker container, I copy the .vscode-docker dir into place. When I am done I remove .vscode and copy the .vscode-local back.

I am sure I could merge the 2 launch.json and task.json files, but for some reason I like keeping them separate.

After updating to the latest version of VS Code (I believe 1.11.1) the format of the launch.json file has changed. The yo docker command produces a “pipeTransport” property in this file but no debuggerPath property – which is now required. When I get that sorted out I will update the instructions on this page to reflect the change.

Summary

By adding the docker generator to your toolbox you can now debug your ASP.NET project inside of a Docker container. Note to control which version of the tools / runtime your project uses simply change the image used by the FROM line in the Dockerfile.debug file.

 

Enjoy!

2 comments

Leave a Reply

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