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

标签 node.js redis visual-studio-code docker-compose vscode-debugger

我的目标是为我的 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/

相关文章:

node.js - Nuxt.js + Express + Heroku — Axios 请求在生产中返回 404,但在开发模式下不在 localhost 上返回 — 有什么解决方案吗?

go - VS Code中main.go以外的调试文件

python - 3000 keyhash 的 Redis 值获取大约需要 10 秒(python 3.5)

python - VSCode 的 vscode-python 扩展是语言服务还是只是扩展?

powershell - 我怎样才能得到一个任务来设置一个可以在配置中使用的变量?

angularjs - 在现有项目中更新 Ionic

javascript - 如何处理异步函数和集合?

node.js - 从服务器获取 json 数据后尝试将其呈现给客户端,但仅显示第一个元素

redis - 该组中的任何人都在使用 Redis 作为数据库