我正在努力理解如何在使用docker时调试应用程序。我找到了这份文档,但结果并非预期: https://code.visualstudio.com/docs/containers/docker-compose
我的做法如下:
dotnet new mvc
- 在VS Code中使用快捷键ctrl + shift + p 添加docker文件到工作空间
- 修改docker-compose.debug.yml以支持SSL
metricdemo:
image: metricdemo
build:
context: .
dockerfile: Dockerfile
ports:
- 80
- 5000:443
environment:
- ASPNETCORE_ENVIRONMENT=Development
- ASPNETCORE_URLS=http://+:80;https://+:443
- ASPNETCORE_Kestrel__Certificates__Default__Password=password
- ASPNETCORE_Kestrel__Certificates__Default__Path=/root/.dotnet/https/aspnetcore-cert.pfx
volumes:
- C:\cert\:/root/.dotnet/https
- ~/.vsdbg:/remote_debugger:rw
下一步是创建启动配置,以便能够附加调试器:
{
"name": "Docker .NET Core Attach (Preview)",
"type": "docker",
"request": "attach",
"platform": "netCore",
"netCore": {
"debuggerPath": "/remote_debugger/vsdbg"
},
"sourceFileMap": {
"/src": "${workspaceFolder}"
}
}
我已经尝试了设置和不设置debuggerPath
两种组合。
之后,我使用以下命令启动docker-compose:
docker-compose -f "docker-compose.debug.yml" up --build
然后我附加调试器。
更新 调试不起作用的原因实际上是Dockerfile本身:
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
WORKDIR /src
COPY ["MetricDemo.csproj", "./"]
RUN dotnet restore "./MetricDemo.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "MetricDemo.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "MetricDemo.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "MetricDemo.dll"]
构建和发布这两个层面都是使用Release模式构建的。将其设置为Debug即可解决问题。现在问题来了: 我如何能够从docker-compose中控制这个变量,以便我可以从docker-compose.debug.yml
构建调试模式并从docker-compose.yml
构建发布配置呢?我不想因为一个项目而需要有两个docker文件。