[速查] 配置vscode远程调试docker中的.net进程

77 阅读1分钟

使用vs连接远程进行调试会自动下载vsdbg到容器,但是由于网路问题,经常失败或者需要等很久,所以想把vsdbg环境提前安装到容器内,参考网上文章后调通了,步骤如下:

安装putty

vscode远程连接使用ssh时,无法在命令行参数中输入密码,比较麻烦,故使用putty,需要下载putty并安装

  • 验证:cmd输入`plink

构建镜像

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY . .
RUN dotnet build -c Release -o /app/build

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

FROM base as final
WORKDIR /app

# 安装ssh server
RUN apt-get update && apt-get install -y --no-install-recommends openssh-server && mkdir -p /run/sshd
# 修改密码
RUN echo "root:123" | chpasswd
# 配置sshd
COPY sshd_config /etc/ssh/sshd_config
# 复制vsdbg程序
COPY vsdbg-linux /vsdbg
COPY --from=publish /app/publish .

EXPOSE 22

CMD /usr/sbin/sshd -D & dotnet ConsoleApp1.dll 

对应sshd_config配置文件:

Port                       22
ListenAddress              0.0.0.0
LoginGraceTime             180
X11Forwarding              yes
Ciphers aes128-cbc,3des-cbc,aes256-cbc
MACs hmac-sha1,hmac-sha1-96
StrictModes                yes
SyslogFacility             DAEMON
PasswordAuthentication     yes
PermitEmptyPasswords       no
PermitRootLogin            yes

vsdbg下载:vsdebugger.azureedge.net/vsdbg-16-3-…

添加远程主机putty连接信任

本机执行:plink -l root -pw 123 localhost -P 9922 -T并同意连接

vscode debug配置

configurations数组下添加:

    {
      "name": ".NET Core Remote Attach",
      "type": "coreclr",
      "request": "attach",
      "processId": "${command:pickRemoteProcess}",
      "pipeTransport": {
        "pipeCwd": "${workspaceFolder}",
        "pipeProgram": "plink",
        "pipeArgs": [ "-l", "root", "-pw", "123", "localhost", "-P", "9922", "-T" ], // putty连接参数
        "debuggerPath": "/vsdbg/vsdbg" // vsdbg在docker容器中的路径
      },
      "sourceFileMap": {
        "/src": "${workspaceFolder}"
      }
    }

运行调试

调试愉快~

参考资料

如何使用Visual Studio Code远程调试部署在linux docker上的.net core应用_vstudioy远程docker调试-CSDN博客