运行Node.js/Swagger 在 docker 中使用 pm2 的应用程序:打开时出错 Api.yaml

Running Node.js/Swagger application with pm2 in docker: Error opening Api.yaml

1.总结:

我创建了一个简单的应用程序,它使用 Node.js、Express 和 Swagger,方法是在 Docker 容器中遵循此 tutorial and using generator-express-no-stress. In addition the application should be executed with pm2。一切正常,直到我启动容器,这在尝试读取 Api.yaml.

时出错

2。步骤:

首先,我创建项目:

yo express-no-stress server
cd server
npm i

对于服务器,我将 localhost 更改为 0.0.0.0 (http.createServer(app).listen(port, '0.0.0.0', welcome(port));),运行 服务器 npm run dev 并执行 curl 0.0.0.0:3000/api/v1/spec, returns - 正如预期的那样 - Swagger 文件的规范。

接下来我在项目中添加一个pm2的配置文件,pm2.json:

{
  "name": "server",
  "script": "build/main.js",
  "env": {
    "APP_ID": "server",
    "PORT": "3000",
    "LOG_LEVEL": "debug",
    "REQUEST_LIMIT": "100kb",
    "SESSION_SECRET": "mySecret"
  }
}

这次我使用pm2启动服务器:

$ pm2 start pm2.json
$ curl 0.0.0.0:3000/api/v1/spec
$ pm2 delete all

再次返回规范。

接下来,我添加一个Docker文件:

FROM node:alpine

COPY build build/
COPY package.json .
COPY pm2.json .

RUN npm i pm2 -g \
  && npm i --production \
  && npm cache verify

CMD ["pm2-runtime", "start", "pm2.json"]

我构建镜像并 运行 容器:

docker build -t server:0.0.1 .
docker run --rm --net=host server:0.0.1

3。问题与疑问

这是我的问题:

[2018-05-30 09:51:49] PM2 log: Launching in no daemon mode
[2018-05-30 09:51:49] PM2 log: Starting execution sequence in -fork mode- for app name:server id:0
[2018-05-30 09:51:49] PM2 log: App name:server id:0 online
{"level":30,"time":1527673910123,"msg":"up and running in development @: r2d2 on port: 3000}","pid":17,"hostname":"r2d2","name":"server","v":1}
Error: Error opening file "///server/common/swagger/Api.yaml" 
ENOENT: no such file or directory, open '///server/common/swagger/Api.yaml'
    at ReadFileContext.callback (/node_modules/json-schema-ref-parser/lib/resolvers/file.js:52:20)
    at FSReqWrap.readFileAfterOpen [as oncomplete] (fs.js:352:13)
Error: ENOENT: no such file or directory, open '///server/common/swagger/Api.yaml'

我不知道为什么会出现错误以及如何修复它。我不知道为什么路径 ///server/common/swagger/Api.yaml 看起来有点奇怪,我什至不知道这是否会导致错误。虽然,容器保持 运行ning。所以:我该如何修复它,或者知道在哪里可以查看?

4.进一步的步骤

但是,我已经完成了以下步骤来检查错误:

curl 0.0.0.0:3000/api/v1/spec 来自我的主机 returns:

<h1>500 Error</h1><pre>Error opening file "///server/common/swagger/Api.yaml" 
ENOENT: no such file or directory, open '///server/common/swagger/Api.yaml'</pre>

进入容器 docker exec -it aacfcc6c5744 sh:

pm2 show server
┌───────────────────┬────────────────────────────────────┐
| status            │ online                             │
│ name              │ server                             │
│ restarts          │ 0                                  │
│ uptime            │ 3m                                 │
│ script path       │ /build/main.js                     │
│ script args       │ N/A                                │
│ error log path    │ /root/.pm2/logs/server-error-0.log │
│ out log path      │ /root/.pm2/logs/server-out-0.log   │
│ pid path          │ /root/.pm2/pids/server-0.pid       │
│ interpreter       │ node                               │
│ interpreter args  │ N/A                                │
│ script id         │ 0                                  │
│ exec cwd          │ /                                  │
│ exec mode         │ fork_mode                          │
│ node.js version   │ 10.2.1                             │
│ watch & reload    │ ✘                                  │
│ unstable restarts │ 0                                  │
│ created at        │ 2018-05-30T09:51:49.825Z           |   
└───────────────────┴────────────────────────────────────┘

并且:

cd build/ && tree
.
├── main.js
├── main.map
├── public
│   ├── api-explorer
│   │   ├── favicon-16x16.png
│   │   ├── favicon-32x32.png
│   │   ├── index.html
│   │   ├── oauth2-redirect.html
│   │   ├── swagger-ui-bundle.js
│   │   ├── swagger-ui-bundle.js.map
│   │   ├── swagger-ui-standalone-preset.js
│   │   ├── swagger-ui-standalone-preset.js.map
│   │   ├── swagger-ui.css
│   │   ├── swagger-ui.css.map
│   │   ├── swagger-ui.js
│   │   └── swagger-ui.js.map
│   └── index.html
└── server
    └── common
        └── swagger
            └── Api.yaml

抱歉问了这么长的问题,但我想确保我的所有步骤都清晰且可重现。如果您需要更多信息,请告诉我。

5.编辑:

问题与pm2有关。如果我进入容器,将应用程序的端口更改为 3001(因为 3000 已被使用)并执行 npm start (cd build && node main) 它可以工作,我可以从我的本地机器访问规范。

问题已解决。我只需要将带有 cwd 的当前工作目录添加到 pm2 的配置文件中:

{
  "name": "server",
  "script": "build/main.js",
  "cwd": "build/",
  "env": {
    "APP_ID": "server",
    "PORT": "3000",
    "LOG_LEVEL": "debug",
    "REQUEST_LIMIT": "100kb",
    "SESSION_SECRET": "mySecret"
  }
}