使用“docker-compose 运行”时无法调试,但“docker-compose up”有效
Can't debug when using `docker-compose run`, but `docker-compose up` works
如果需要,我可以创建一个测试仓库。但这个问题的要点是,当我使用 up
命令时,我能够调试由 docker-compose 创建的 Docker 图像。使用 run
命令时,我无法调试同一个容器。在这两种情况下,我都可以看到我的应用程序启动。使用 docker-compose run
.
时,vs 代码调试器似乎无法连接到 docker 容器
这类似于我在 vs 代码中的工作启动任务(调试器可以连接到的任务):
{
"type": "node",
"request": "launch",
"name": "Docker: Debug",
"runtimeExecutable": "docker-compose",
"runtimeArgs": [
"up"
],
"port": 9229,
"stopOnEntry": true,
"restart": true,
"timeout": 30000,
"localRoot": "${workspaceRoot}",
"remoteRoot": "/app",
"outFiles": [ "${workspaceRoot}/dist/**/*.js" ],
"console": "externalTerminal",
"internalConsoleOptions": "neverOpen",
"protocol": "inspector",
"showAsyncStacks": true
}
我的docker-compose文件也在这里指定了一个命令(例如npm run start-app
),其中start-app
有如下定义:
nodemon --legacy-watch --watch ./dist/app1 --inspect=0.0.0.0:9229 --nolazy ./dist/app1/index.js
为什么我有问题:
我不想在 docker-compose.yml
文件中指定命令,因为我有一个包含多个进程的图像。 (每个进程将有一个容器,但它们都共享相同的图像。)我目前正在为 kubernetes 部署和本地开发工作 run/debug。我想通过使用指定命令启动容器来在容器中获得此 运行 宁和调试。
我希望能够 运行 容器并在我的 vs 代码 launch.json
文件中指定要 运行 的应用程序,类似于以下内容:
{
"type": "node",
"request": "launch",
"name": "Docker: Debug",
"runtimeExecutable": "docker-compose",
"runtimeArgs": [
"run",
"all",
"npm",
"run",
"start-app"
],
"port": 9229,
"stopOnEntry": true,
"restart": true,
"timeout": 30000,
"localRoot": "${workspaceRoot}",
"remoteRoot": "/app",
"outFiles": [ "${workspaceRoot}/dist/**/*.js" ],
"console": "externalTerminal",
"internalConsoleOptions": "neverOpen",
"protocol": "inspector",
"showAsyncStacks": true
}
仅供参考,arg 列表中的 all
指的是 docker-compose.yml
文件中我的服务名称。
一位同事在 docker-compose run
的文档中发现了以下片段:
...the docker-compose run command does not create any of the ports specified in the service configuration. This prevents port collisions with already-open ports. If you do want the service's ports to be created and mapped to the host, specify the --service-ports flag
所以我更改了启动任务的参数以包含此标志,它现在可以工作了。
"runtimeArgs": [
"run",
"--service-ports",
"all",
"npm",
"run",
"start-app"
],
再次说明,--service-ports
告诉 docker compose 遵从我在 docker-compose.yml 文件中的端口映射,up
命令默认情况下。 all
是我在docker-compose.yml文件中的服务名称,其余命令是npm run start-app
,其中start-app
是我自定义的脚本package.json 文件。
如果需要,我可以创建一个测试仓库。但这个问题的要点是,当我使用 up
命令时,我能够调试由 docker-compose 创建的 Docker 图像。使用 run
命令时,我无法调试同一个容器。在这两种情况下,我都可以看到我的应用程序启动。使用 docker-compose run
.
这类似于我在 vs 代码中的工作启动任务(调试器可以连接到的任务):
{
"type": "node",
"request": "launch",
"name": "Docker: Debug",
"runtimeExecutable": "docker-compose",
"runtimeArgs": [
"up"
],
"port": 9229,
"stopOnEntry": true,
"restart": true,
"timeout": 30000,
"localRoot": "${workspaceRoot}",
"remoteRoot": "/app",
"outFiles": [ "${workspaceRoot}/dist/**/*.js" ],
"console": "externalTerminal",
"internalConsoleOptions": "neverOpen",
"protocol": "inspector",
"showAsyncStacks": true
}
我的docker-compose文件也在这里指定了一个命令(例如npm run start-app
),其中start-app
有如下定义:
nodemon --legacy-watch --watch ./dist/app1 --inspect=0.0.0.0:9229 --nolazy ./dist/app1/index.js
为什么我有问题:
我不想在 docker-compose.yml
文件中指定命令,因为我有一个包含多个进程的图像。 (每个进程将有一个容器,但它们都共享相同的图像。)我目前正在为 kubernetes 部署和本地开发工作 run/debug。我想通过使用指定命令启动容器来在容器中获得此 运行 宁和调试。
我希望能够 运行 容器并在我的 vs 代码 launch.json
文件中指定要 运行 的应用程序,类似于以下内容:
{
"type": "node",
"request": "launch",
"name": "Docker: Debug",
"runtimeExecutable": "docker-compose",
"runtimeArgs": [
"run",
"all",
"npm",
"run",
"start-app"
],
"port": 9229,
"stopOnEntry": true,
"restart": true,
"timeout": 30000,
"localRoot": "${workspaceRoot}",
"remoteRoot": "/app",
"outFiles": [ "${workspaceRoot}/dist/**/*.js" ],
"console": "externalTerminal",
"internalConsoleOptions": "neverOpen",
"protocol": "inspector",
"showAsyncStacks": true
}
仅供参考,arg 列表中的 all
指的是 docker-compose.yml
文件中我的服务名称。
一位同事在 docker-compose run
的文档中发现了以下片段:
...the docker-compose run command does not create any of the ports specified in the service configuration. This prevents port collisions with already-open ports. If you do want the service's ports to be created and mapped to the host, specify the --service-ports flag
所以我更改了启动任务的参数以包含此标志,它现在可以工作了。
"runtimeArgs": [
"run",
"--service-ports",
"all",
"npm",
"run",
"start-app"
],
再次说明,--service-ports
告诉 docker compose 遵从我在 docker-compose.yml 文件中的端口映射,up
命令默认情况下。 all
是我在docker-compose.yml文件中的服务名称,其余命令是npm run start-app
,其中start-app
是我自定义的脚本package.json 文件。