【问题标题】:pm2 running in docker transpiler not working在 docker transpiler 中运行的 pm2 无法正常工作
【发布时间】:2018-04-23 02:14:47
【问题描述】:

感谢您花时间帮我解决这个问题。

  • 简介

我已经创建了一个 docker 并且我还为 pm2 配置了一个配置文件。我希望能够在开发模式下启动 docker 并在里面使用 pm2,能够通过开发工具等调试 typescript。

应用程序是使用nodejs,typescript中的expressjs构建的。

  • 问题 在本地运行 pm2 start server-debug.config.js 一切正常。我能够通过开发工具调试应用程序,我能够打开打字稿文件并添加断点。

在构建 docker 时,一切正常,但有一个例外。打开开发工具,只能看到js文件,无法调试打字稿。

所以基本上转译器在本地工作,但不能通过 docker 远程工作。

  • 配置

    1. Docker 文件

```

# specify the node base image with your desired version node:<version>
FROM keymetrics/pm2:latest-alpine

# client backend
COPY . /home/api
WORKDIR /home/api

ARG API_TAG
ARG NODE_ENV
ARG CONTAINER_USER
ENV NODE_ENV $NODE_ENV

RUN npm install && npm install pm2 -g && pm2 install typescript

# Set user to node on production
USER $CONTAINER_USER

# start backend & frontend
ENTRYPOINT if [ "$NODE_ENV" = "development" ];\
then pm2-runtime /home/api/server-dev.config.js --env ${NODE_ENV} --no- 
daemon;\
else pm2-runtime /home/api/server.config.js --env ${NODE_ENV} --no-daemon;\
fi
  1. PM2 配置文件

```

const _ = require('lodash');
const serverConfig = require('./server.config');

const devServerConfig = {apps: []};

devServerConfig.apps[0] = _.merge(serverConfig.apps[0], {
    watch: true,
    port: 8000,
    source_map_support: true,
    script: "./dist/App.js",
    ignore_watch: ["data",
    "node_modules",
    "DockerFile",
    ".gitignore",
    "docker-compose*",
    "README.md",
    "server.config.js",
    "logs",
    "server-dev.config.js"],
    node_args: [ "--inspect=0.0.0.0:5858", "--harmony" ],
    error_file : "./logs/error.service-debug.log",
    out_file : "./logs/out.service-debug.log",
    pid_file : "./logs/debug.service.pid",
    log_date_format: "",
    watch_options: {
        "usePolling": true,
        "alwaysStat": true,
        "useFsEvents": false,
    },
})

// , "--require=ts-node/register", "--harmony"

module.exports = devServerConfig;

```

  1. .env 文件

    API_TAG=v1.0 NODE_ENV=发展 CONTAINER_USER=root 端口=8000 MONGO_PORT=27017

  2. tsconfig

```

{
    "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "outDir": "dist",
    "sourceMap": true
    },
    "files": [
        "./node_modules/@types/mocha/index.d.ts",
        "./node_modules/@types/node/index.d.ts"
    ],
    "include": [
        "./src/**/*.ts"
    ],
    "exclude": [
    "node_modules"
    ]
 }

```

【问题讨论】:

  • --no-daemon 不需要 pm2-runtime?

标签: node.js express docker pm2


【解决方案1】:

对于任何在这个问题上苦苦挣扎的人,经过数小时的调试、反复试验,我找到了一个可行的解决方案。在 tsconfig 文件中添加内联源映射将解决此问题。

我将接受我自己的答案作为帮助他人解决此问题的解决方案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-04
    • 1970-01-01
    相关资源
    最近更新 更多