Git Product home page Git Product logo

docker.dotnet.debug's Introduction

This is a sample that demonstrates how to use vscode to build and debug dotnet core 2.0 console application in docker container.

Our goal

What we're going to achieve is to be able to build our dotnet core application or library inside a Docker container, so we don't need to install an appropriate sdk on our host machine. After we successfully build an image with the app, we need to spin up a new container and attach debugger to our app inside this container.

Step-by-step guidline

  1. First of all we need to create a docker file that will describe how we are going to build a Docker image with our application.
FROM microsoft/dotnet:2.1-sdk
ENV NUGET_XMLDOC_MODE skip
WORKDIR /vsdbg

# Installing vsdbg debbuger into our container 
RUN apt-get update \
    && apt-get install -y --no-install-recommends \
       unzip \
    && rm -rf /var/lib/apt/lists/* \
    && curl -sSL https://aka.ms/getvsdbgsh | bash /dev/stdin -v latest -l /vsdbg

# Copying source files into container and publish it
RUN mkdir /app
WORKDIR /app

COPY App.csproj /app
RUN dotnet restore

COPY . /app
RUN dotnet publish -c Debug -o out

# Kick off a container just to wait debugger to attach and run the app
ENTRYPOINT ["/bin/bash", "-c", "sleep infinity"]
  1. Then we create a script file dockerTask.sh that will contain utility task buildForDebug. This task is responsible for:

    • stopping and removing old container(s)
    • building a new image
    • running a new container
  2. Add task configuration to tasks.json

{
    "taskName": "buildForDebug",
    "suppressTaskName": true,
    "args": [
        "-c",
        "./scripts/dockerTask.sh buildForDebug"
    ],
    "isBuildCommand": false,
    "showOutput": "always"
}
  1. And the last step is to create the launch configuration in launch.json, that will use task created on the previous step as a preLaunchTask.
{
    "name": ".NET Core Docker Launch (console)",
    "type": "coreclr",
    "request": "launch",                    # we are going to run a new instance of our application
    "preLaunchTask": "buildForDebug",       # name of the task that will build and run a container
    "program": "/app/out/App.dll",          # path to program to run inside a container
    "cwd": "/app/out",                      # working directory inside a container
    "sourceFileMap": {
        "/app": "${workspaceRoot}/src"      # mapping of source code inside a container to the source code on a host machine
    },
    "pipeTransport": {
        "pipeProgram": "docker",            # use Docker as a pipe program
        "pipeCwd": "${workspaceRoot}",
        "pipeArgs": [
            "exec -i docker.dotnet.debug_1" # attach to container and execute command of running app with attached debbuger
        ],
        "quoteArgs": false,
        "debuggerPath": "/vsdbg/vsdbg"      # path to installed debugger inside a container
    }
}

And that's basically it. For more details and the full example see the code in the repo.

Tested environment:

  • OS: macOS High Sierra 10.13.6
  • Docker: 18.05.0-ce-mac67 (25042)
  • dotnet: 2.1
  • vscode: Version 1.26.0-insider
  • ms-vscode.csharp: 1.15.2

Known issues (or features?!):

  1. We run container in 'runContainer' task, but we start app in this container only when we decided to debug it. So, in order to do that, we have to attach to the running container and provide the name of container in launch.json. By doing that we have to keep in sync the name of the container in two files. That's not that elegant solution, and definitely is a violation of SRP.
  2. The other problem that arise from the #1 is that we leave container running even when we finished debugging.

For more details read these articles:

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.