调试容器化应用程序

Docker扩展提供了更多支持,用于在Docker容器内调试应用程序,例如为将调试器附加到在容器内运行的应用程序提供launch.json配置的脚手架。

Docker 扩展提供了一个 docker 调试配置提供程序,该程序管理 VS Code 如何启动应用程序和/或将调试器附加到运行中的 Docker 容器中的应用程序。此提供程序通过 launch.json 中的条目进行配置,配置特定于提供程序支持的每个应用程序平台。

Docker扩展目前支持在Docker容器内调试Node.jsPython.NET应用程序。

需求

将启动配置脚手架或粘贴到launch.json不足以构建和调试Docker容器。要成功运行Docker启动配置,您必须拥有:

  • 一个Dockerfile。
  • docker-builddocker-run 任务在 tasks.json 中。
  • 一个调用这些任务的启动配置。

我们建议使用Docker: Add Docker Files to Workspace...命令来创建这些项目,如果这些资产尚不存在。如果您已经有一个可用的Dockerfile,我们建议使用Docker: Initialize for Docker debugging命令来搭建启动配置和与Docker相关的任务。

Node.js

有关在Docker容器内调试Node.js应用程序的更多信息,请访问在容器内调试Node.js

调试Node.js应用程序的launch.json配置示例:

{
  "configurations": [
    {
      "name": "Docker Node.js Launch",
      "type": "docker",
      "request": "launch",
      "preLaunchTask": "docker-run: debug",
      "platform": "node"
    }
  ]
}

Python

有关在Docker容器内调试Python应用程序的更多信息,请访问在容器内调试Python

调试Python应用程序的launch.json配置示例:

{
  "configurations": [
    {
      "name": "Docker: Python - Django",
      "type": "docker",
      "request": "launch",
      "preLaunchTask": "docker-run: debug",
      "python": {
        "pathMappings": [
          {
            "localRoot": "${workspaceFolder}",
            "remoteRoot": "/app"
          }
        ],
        "projectType": "django"
      }
    }
  ]
}

.NET

您可以选择在Docker容器内构建和调试项目的两种方式:

  • 使用 .NET SDK:如果您熟悉MSBuild或希望在不使用Dockerfile的情况下将项目容器化,这是推荐的选择。

    注意: 此选项仅适用于 .NET SDK 7 及以上版本,并使用 dotnet publish 命令来构建镜像。

  • 使用Dockerfile:如果您更喜欢使用Dockerfile自定义您的项目,请选择此选项。

有关这两个选项的更多详细信息,请参阅在Docker容器中调试.NET

使用Dockerfile调试.NET应用程序的launch.json配置示例:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Launch .NET Core in Docker",
      "type": "docker",
      "request": "launch",
      "preLaunchTask": "Run Docker Container",
      "netCore": {
        "appProject": "${workspaceFolder}/project.csproj"
      }
    }
  ]
}

配置参考

Property Description
containerName Name of the container used for debugging.
dockerServerReadyAction Options for launching a browser to the Docker container. Similar to serverReadyAction, but replaces container ports with host ports.
removeContainerAfterDebug Whether to remove the debug container after debugging.
platform The target platform for the application. Can be netCore or node.
netCore Options for debugging .NET projects in Docker.
node Options for debugging Node.js projects in Docker.
python Options for debugging Python projects in Docker.

dockerServerReadyAction 对象属性

Property Description
action The action to take when the pattern is found. Can be debugWithChrome or openExternally.
containerName The container name to match the host port.
pattern The regex pattern to look for in Debug console output.
uriFormat The URI format to launch.
webRoot The root folder from which web pages are served. Used only when action is set to debugWithChrome.

节点对象属性

这些属性与VS Code文档中描述的将调试器附加到Node.js应用程序的属性相同。所有在node对象中传递的属性都将传递给Node.js调试适配器,即使未在下面特别列出。

Property Description Default
port Optional. The debug port to use. 9229
address Optional. TCP/IP address of the debug port.
sourceMaps Optional. Enable source maps by setting this to true.
outFiles Optional. Array of glob patterns for locating generated JavaScript files.
autoAttachChildProcesses Optional. Track all subprocesses of debuggee and automatically attach to those that are launched in debug mode.
timeout Optional. When restarting a session, give up after this number of milliseconds.
stopOnEntry Optional. Break immediately when the program launches.
localRoot Optional. VS Code's root directory. The root workspace folder.
remoteRoot Optional. Node's root directory within the Docker container. /usr/src/app
smartStep Optional. Try to automatically step over code that doesn't map to source files.
skipFiles Optional. Automatically skip files covered by these glob patterns.
trace Optional. Enable diagnostic output.

Python 对象属性

Property Description Default
host The host for remote debugging.
port The port for remote debugging. 5678
pathMappings Maps the project path between local machine and remote host.
projectType The type of your Python project, flask for Flask projects, django for Django, fastapi for FastAPI, and general for others. The project type will be used to set the port and commands used for debugging.
justMyCode Debug only user-written code.
django Django debugging. false
jinja Jinja template debugging (such as Flask). false

netCore 对象属性

netCore对象中传递的属性通常会传递给.NET调试适配器,即使下面没有特别列出。完整的调试器属性列表在OmniSharp VS Code扩展文档中。

Property Description
appProject The .NET project (.csproj, .fsproj, etc.) to debug.

下一步

继续阅读以了解更多关于: