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.
FROM microsoft/dotnet:2.1-sdk
ENV NUGET_XMLDOC_MODE skip
WORKDIR /vsdbg
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
RUN mkdir /app WORKDIR /app
COPY App.csproj /app RUN dotnet restore
COPY . /app RUN dotnet publish -c Debug -o out
ENTRYPOINT ["/bin/bash", "-c", "sleep infinity"]
2. 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
3. Add task configuration to tasks.json
```json
{
"taskName": "buildForDebug",
"suppressTaskName": true,
"args": [
"-c",
"./scripts/dockerTask.sh buildForDebug"
],
"isBuildCommand": false,
"showOutput": "always"
}
{
"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:
Known issues (or features?!):
For more details read these articles: