Debug dockerized .NET application on remote machine using Visual Studio Code

Dominik Deschner
medialesson
Published in
3 min readJan 31, 2023

Developing and deploying containerized applications is a common and convenient way, especially when you are building IoT solutions running on the edge/field. In some projects you develop software that connects against real hardware/services that are not globally available to your team. So it can be beneficial when developers can directly debug the application running on the testing system.

Even-though debugging a remote containerized .NET application is not that complex it is neither really intuitive that’s why I am guiding you through the process in this post. As a precondition we assume that you have VSCode and docker-cli installed on your local machine and a working SSH client.

First of all we need to assure that the remote machine where our docker container is running can be reached via SSH with keyfile authentication. Therefor we create an alias for the ssh connection in our “~/.ssh/config” file according to the following example

host dockerhost
HostName 1.3.3.7
User userwithdockeraccess
IdentityFile ~/.ssh/id_rsa

“dockerhost” is the alias for our remote machine with ip “1.3.3.7” where we provide user and a keyfile to authenticate. After saving the file you should automatically get logged in when you type “ssh dockerhost”.

Next we will create a new docker context on our local machine and tell docker-cli to use it, in order to connect our docker-cli with the remote docker engine:

docker context create DevRaspi --description "DevRaspi" --docker "host=ssh://dockerhost"
docker context use DevRaspi

If not already done we need to embed vsdbg into our image, this can be achieved by extending the Dockerfile with the following:

RUN [ "$BuildConfig" = "Debug" ] && \
apt update && \
apt install unzip && \
curl -sSL https://aka.ms/getvsdbgsh | /bin/sh /dev/stdin -v latest -l /vsdbg;d

Here is the whole file as an example:

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build-env
ARG BuildConfig=Release
WORKDIR /app

# Copy everything else and build
COPY ./ ./

RUN [ "$BuildConfig" = "Debug" ] && \
apt update && \
apt install unzip && \
curl -sSL https://aka.ms/getvsdbgsh | /bin/sh /dev/stdin -v latest -l /vsdbg;

RUN dotnet restore ./xxx.csproj
RUN dotnet publish ./xxx -c ${BuildConfig} -o out

# Build runtime image
FROM mcr.microsoft.com/dotnet/runtime:6.0
WORKDIR /app
COPY --from=build-env /app/out .
COPY --from=build-env /vsdbg /vsdbg
ENTRYPOINT ["dotnet", "xxx.dll"]

Now we just need to rebuild the container and start it via the docker cli and attach our local debugger to the container. In order to achieve that we can extend our Visual Studio Codes “launch.json” with the following entry:

    {
"name": "Docker .NET Core Attach (Preview)",
"type": "docker",
"request": "attach",
"platform": "netCore",
"netCore": {
"debuggerPath": "/vsdbg/vsdbg"
},
"sourceFileMap": {
"/app": "${workspaceFolder}/services/"
}

Now we can select and run our newly created launch configuration. VS Code will request us to select the desired container in order to attach the debugger. And there you go now you should have your VSCode attached to a dotnet process running in a remote docker container.

--

--