在容器中调试 Python
当向 Python 项目添加 Docker 文件时,会添加任务和启动配置,以支持在 Docker 容器中调试应用程序。为了适应 Python 项目的各种场景,某些应用程序可能需要额外的配置。
配置 Docker 容器入口点
您可以通过在 tasks.json
中设置属性来配置 Docker 容器的入口点。当您首次使用Docker: 向工作区添加 Docker 文件...命令时,VS Code 会自动配置容器入口点。
示例:配置 Python 模块的入口点
{
"tasks": [
{
"type": "docker-run",
"label": "docker-run: debug",
"dependsOn": ["docker-build"],
"python": {
"module": "myapp"
}
}
]
}
示例:配置 Python 文件的入口点
{
"tasks": [
{
"type": "docker-run",
"label": "docker-run: debug",
"dependsOn": ["docker-build"],
"python": {
"args": ["runserver", "0.0.0.0:8000", "--nothreading", "--noreload"],
"file": "manage.py"
}
}
]
}
自动启动浏览器到应用程序的入口页面
您可以选择 Docker: Python - Django 或 Docker: Python - Flask 启动配置,以自动启动浏览器到应用程序的主页。默认情况下启用此功能,但您可以通过在 launch.json
中设置 dockerServerReadyAction
对象来显式配置此行为。
此功能取决于应用程序的几个方面
- 应用程序必须输出到调试控制台或 Docker 日志。
- 应用程序必须记录“服务器就绪”消息。
- 应用程序必须提供可浏览的页面。
以下是使用 dockerServerReadyAction
启动浏览器以打开 about.html
页面(基于特定的服务器消息模式)的示例
{
"configurations": [
{
"name": "Docker: Python - Django",
"type": "docker",
"request": "launch",
"preLaunchTask": "docker-run: debug",
"python": {
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "/app"
}
],
"projectType": "django"
},
"dockerServerReadyAction": {
"action": "openExternally",
"pattern": "Starting development server at (https?://\\S+|[0-9]+)",
"uriFormat": "%s://127.0.0.1:%s/about.html"
}
}
]
}
注意:在
pattern
属性中找到的正则表达式只是尝试捕获类似于“Starting development server athttps://127.0.0.1:8000
”的日志消息。它适应 http 或 https 的 url、任何主机名和任何端口的变体。
重要的 dockerServerReadyAction 对象属性
-
action
:找到模式时要采取的操作。可以是debugWithChrome
或openExternally
。 -
pattern
:如果应用程序记录的消息与上面显示的消息不同,请将 dockerServerReadyAction 对象的pattern
属性设置为与该消息匹配的 JavaScript 正则表达式。正则表达式应包含一个捕获组,该捕获组对应于应用程序正在侦听的端口。 -
uriFormat
:默认情况下,Docker 扩展将打开浏览器的主页(但这由应用程序决定)。如果您希望浏览器打开特定页面(如上面的示例),则应将 dockerServerReadyAction 对象的uriFormat
属性设置为格式字符串,其中包含两个字符串标记,以指示协议和端口替换。
如何在 Django 或 Flask 应用中启用热重载
当您为 Django 或 Flask 选择Docker: 向工作区添加 Docker 文件时,我们会为您提供为静态部署配置的 Dockerfile 和 tasks.json
。每次您更改应用程序代码时,都需要重建并重新运行容器。热重载允许您在容器继续运行时可视化应用程序代码中的更改。按照以下步骤启用热重载
对于 Django 应用
-
在 Dockerfile 中,注释掉将应用程序代码添加到容器的行。
#ADD . /app
-
在
tasks.json
文件中的docker-run
任务中,创建一个新的dockerRun
属性,其中包含volumes
属性。此设置创建从当前工作区文件夹(应用程序代码)到容器中/app
文件夹的映射。{ "type": "docker-run", "label": "docker-run: debug", "dependsOn": [ "docker-build" ], "dockerRun": { "volumes": [ { "containerPath": "/app", "localPath": "${workspaceFolder}" } ] }, ... }
-
通过删除
--noreload
和--nothreading
编辑 python 属性。{ ... "dockerRun": { "volumes": [ { "containerPath": "/app", "localPath": "${workspaceFolder}" } ] }, "python": { "args": [ "runserver", "0.0.0.0:8000", ], "file": "manage.py" } }
-
选择 Docker: Python – Django 启动配置并点击 F5 以构建并运行容器。
-
修改并保存任何文件。
-
刷新浏览器并验证是否已进行更改。
对于 Flask 应用
-
在 Dockerfile 中,注释掉将应用程序代码添加到容器的行。
#ADD . /app
-
在
tasks.json
文件中的docker-run
任务中,编辑现有的 dockerRun 属性,方法是在env
属性中添加FLASK_ENV
以及volumes
属性。此设置创建从当前工作区文件夹(应用程序代码)到容器中/app
文件夹的映射。{ "type": "docker-run", "label": "docker-run: debug", "dependsOn": [ "docker-build" ], "dockerRun": { "env": { "FLASK_APP": "path_to/flask_entry_point.py", "FLASK_ENV": "development" }, "volumes": [ { "containerPath": "/app", "localPath": "${workspaceFolder}" } ] }, ... }
-
通过删除
--no-reload
和--no-debugger
编辑 python 属性。{ ... "dockerRun": { "env": { "FLASK_APP": "path_to/flask_entry_point.py", "FLASK_ENV": "development" }, "volumes": [ { "containerPath": "/app", "localPath": "${workspaceFolder}" } ] }, "python": { "args": [ "run", "--host", "0.0.0.0", "--port", "5000" ], "module": "flask" } }
-
选择 Docker: Python – Flask 启动配置并点击 F5 以构建并运行容器。
-
修改并保存任何文件。
-
刷新浏览器并验证是否已进行更改。
如何一起构建和运行容器
- 在前面提到的
tasks.json
文件中,存在对docker-build
任务的依赖。该任务是tasks.json
中tasks
数组的一部分。例如
"tasks":
[
{
...
},
{
"label": "docker-build",
"type": "docker-build",
"dockerBuild": {
"context": "${workspaceFolder}",
"dockerfile": "${workspaceFolder}/Dockerfile",
"tag": "YOUR_IMAGE_NAME:YOUR_IMAGE_TAG"
}
}
]
提示:正如依赖项清楚地声明 docker-build
作为其依赖项一样,名称必须与此任务匹配。如果需要,您可以更改名称。
-
JSON 中的
dockerBuild
对象允许以下参数- context:Docker 构建上下文,从中调用您的 Dockerfile
- dockerfile:要执行的 Dockerfile 的路径
- tag:要构建的映像的名称,及其版本标记
-
总的来说,用于构建和调试 Flask 应用程序的 VS Code 设置可以是
-
launch.json
{ "version": "0.2.0", "configurations": [ { "name": "Debug Flask App", "type": "docker", "request": "launch", "preLaunchTask": "docker-run: debug", "python": { "pathMappings": [ { "localRoot": "${workspaceFolder}", "remoteRoot": "/app" } ], "projectType": "flask" }, "dockerServerReadyAction": { "action": "openExternally", "pattern": "Running on (http?://\\S+|[0-9]+)", "uriFormat": "%s://127.0.0.1:%s/" } } ] }
-
tasks.json
{ "version": "2.0.0", "tasks": [ { "type": "docker-run", "label": "docker-run: debug", "dependsOn": ["docker-build"], "dockerRun": { "containerName": "YOUR_IMAGE_NAME", "image": "YOUR_IMAGE_NAME:YOUR_IMAGE_TAG", "env": { "FLASK_APP": "path_to/flask_entry_point.py", "FLASK_ENV": "development" }, "volumes": [ { "containerPath": "/app", "localPath": "${workspaceFolder}" } ], "ports": [ { "containerPort": 5000, "hostPort": 5000 } ] }, "python": { "args": ["run", "--host", "0.0.0.0", "--port", "5000"], "module": "flask" } }, { "label": "docker-build", "type": "docker-build", "dockerBuild": { "context": "${workspaceFolder}", "dockerfile": "${workspaceFolder}/Dockerfile", "tag": "YOUR_IMAGE_NAME:YOUR_IMAGE_TAG" } } ] }
-
后续步骤
了解更多关于