'Debugging docker image in VS code : cs file cannot be found:

I am trying to debug a docker image using the given description in this article.

I have created a Dockerfile like this :

    FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS base

RUN apt update && \
    apt install procps -y && \
    apt install unzip && \
    curl -sSL https://aka.ms/getvsdbgsh | /bin/sh /dev/stdin -v latest -l /vsdbg

WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
WORKDIR /src

COPY *.sln ./
COPY server/Business/NotificationModule.Business.csproj server/Business/
COPY server/Common/NotificationModule.Common.csproj server/Common/
COPY server/Data/NotificationModule.Data.csproj server/Data/
COPY server/DomainModel/NotificationModule.DomainModel.csproj server/DomainModel/
COPY server/Host/NotificationModule.Host.csproj server/Host/

RUN dotnet restore
COPY . .
WORKDIR "/src/server/Host/"
RUN dotnet build "NotificationModule.Host.csproj" -c Debug -o /app/build

FROM build AS publish
RUN dotnet publish "NotificationModule.Host.csproj" -c Debug -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "NotificationModule.dll"]

and I have added the entry for debugging docker images in launch.json like this :

{
            "name": ".NET Core Docker Attach",
            "type": "coreclr",
            "request": "attach",
            "processId": "${command:pickRemoteProcess}",
            "pipeTransport": {
                "pipeProgram": "docker",
                "pipeArgs": [ "exec", "-i", "objective_torvalds" ],
                "debuggerPath": "/vsdbg/vsdbg",
                "pipeCwd": "${workspaceRoot}",
                "quoteArgs": false
            }
        },

It seems that it is working and I can attach the debugger to my process but the problem is that when the debugger hits a breakpoint it can not find any cs files and shows an empty cs file instead.

I would like to ask if you know what I have done wrong.

UPDATE : I have noticed that the debugger is looking for my cs files under src folder which apparantly doens't exist neither in my working directory nor in the image itself . So the question is why it is looking there.



Solution 1:[1]

OK I've got it . That was my mistake because I was using the same docker file that we have for production to copy pdb files into the docker image and those pdb files have been built on a docker container in a src directory so it was looking there .

I just copied the files from my bin/debug into the docker image and now it is workng perfectly.( and later I noticed that it was also mentioned in the article).

so here is the new DockerFile :

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS base

RUN apt update && \
    apt install procps -y && \
    apt install unzip && \
    curl -sSL https://aka.ms/getvsdbgsh | /bin/sh /dev/stdin -v latest -l /vsdbg

WORKDIR /app
EXPOSE 80
EXPOSE 443

COPY bin/debug .

ENTRYPOINT ["dotnet", "NotificationModule.dll"]

and BTW you shouldn't add bin into your .dockerignore file

Solution 2:[2]

I had the same problem - debugger worked (I could see variables values), but the debugger couldn't find my .cs files:

enter image description here

It turned out that the default value of sourceFileMap in launch.json was incorrect, I found information how to use it properly at OmniSharp Github.

Here's how to fix the issue:

After I clicked Create File button in the popup with the error I was able to check where the debugger was looking for the source file. In my case the error message was:

Unable to write file '/out/src/MyApp/WebDriverFactory/WebDriverFactory.cs' (Unknown (FileSystemError): Error: EROFS: read-only file system, mkdir '/out')

I could use the message above to use fix my paths in sourceFileMap:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Docker .NET Core Attach (Preview)",
            "type": "docker",
            "request": "attach",
            "platform": "netCore",
            "sourceFileMap": {
                "/out/src/": "${workspaceFolder}/src"
            }
        }
    ]
}

I used .NET 6 with Alpine images: mcr.microsoft.com/dotnet/runtime:6.0-alpine3.14-amd64

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Beatles1692
Solution 2 ?ukasz Sypniewski