gpt4 book ai didi

node.js - 在 VS 代码调试器中配置设置以运行 docker

转载 作者:IT王子 更新时间:2023-10-29 06:06:04 25 4
gpt4 key购买 nike

我的目标是为我的 VS 代码调试器设置配置。
我有一个遗留代码是使用 docker 容器来运行 redis,并使用 gulp task runner 来启动应用程序。我的工作流程包括我在终端中键入的以下命令:

docker-compose up 使用 db 运行 redis

gulp default 启动应用程序的服务器

到目前为止,我已经成功地为 gulp 任务创建了配置,但仍然难以在调试器中为 docker 设置配置。

Docker Desktop 在 Windows 10 Pro 本地安装

Docker 版本:2.0.0.0-win81 (29211)

docker 文件:docker-compose.yml

version: "2"
services:
redis:
container_name: redis
image: redis:latest
ports:
- 7113:6379
mariadb-test:
container_name: mariadb-test
image: wodby/mariadb
ports:
- 6604:3306
environment:
- MYSQL_ROOT_PASSWORD=***
- MYSQL_USER=****_****
- MYSQL_PASSWORD=****
- MYSQL_DATABASE=****
- MYSQL_CHARACTER_SET_FILESYSTEM=utf8mb4
- MYSQL_CHARACTER_SET_SERVER=utf8mb4
- MYSQL_CLIENT_DEFAULT_CHARACTER_SET=utf8mb4
- MYSQL_COLLATION_SERVER=utf8mb4_unicode_ci
- MYSQL_INIT_CONNECT=SET NAMES utf8mb4

Gulp 任务

gulp.task('default', function (done) {
runSequence('build:server', 'watch', 'start');
});

gulp.task('build:server', function (done) {
var tsProject = tsc.createProject('tsconfig.json');
var tsResult = gulp.src(['server/**/*.ts', 'server/**/*.tsx', '!server/test/**/*'])
.pipe(cache('typescript'))
.pipe(sourcemaps.init())
.pipe(tsProject()).js
.pipe(sourcemaps.mapSources(function (sourcePath, file) {
return sourcePath.replace('../../', '../');
}))
.pipe(sourcemaps.write("."))
.pipe(gulp.dest('dist/server'))
.on('end', done);
});

gulp.task('watch', function () {
gulp.watch(['server/**/*.ts', 'server/**/*.tsx', '!server/desktop/**/*', '!server/test/**/*'], ['compile']).on('change', function (e) {
gutil.log( gutil.colors.blue.bold('[CHANGE] ') + gutil.colors.green(e.path));
});
});

gulp.task('start', function () {
nodemon({
script: 'dist/server/bin.js',
watch: 'dist/',
ext: 'html js',
tasks: []
});
});

我的 launch.json 配置

{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Gulp",
"program": "${workspaceFolder}/node_modules/gulp/bin/gulp.js",
"args": [
"default"
]
},
{
"type": "node",
"request": "attach",
"name": "Docker",
"address": "localhost",
"port": 6379,
"localRoot": "${workspaceFolder}",
"remoteRoot": "/",
}
]
}

我一直收到这个错误:

Error: Cannot connect to runtime process, timeout after 10000 ms - (reason: Cannot connect to the target: connect ECONNREFUSED 127.0.0.1:6379).

最佳答案

因为您不在 docker 中运行您的应用程序,所以您不需要摆弄它。但是,当您启动 Node 进程时,您需要将 --inspect--inspect-brk 添加到 gulp 任务中。

我建议为您的 gulpfile 创建一个新的 debug 任务:

gulp.task('debug', function (done) {
runSequence('build:server', 'watch', 'start:debug');
});

gulp.task('start:debug', function () {
nodemon({
script: 'dist/server/bin.js',
watch: 'dist/',
nodeArgs: ['--inspect'] // or --inspect-brk
ext: 'html js',
tasks: []
});
});

然后您应该能够使用 chrome 调试您的代码,如下所示:https://blog.risingstack.com/how-to-debug-a-node-js-app-in-a-docker-container/

请注意,根据 to this question ,您应该使用 nodemon 1.12.7 或更高版本才能使其正常工作。

关于node.js - 在 VS 代码调试器中配置设置以运行 docker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53701637/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com