简体   繁体   English

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

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

1. Summary : 1.摘要

I have created a simple application, which uses Node.js, Express and Swagger, by following this tutorial and using generator-express-no-stress . 通过遵循本教程并使用generator-express-no-stress ,我创建了一个使用Node.js,Express和Swagger的简单应用程序。 In addition the application should be executed with pm2 wihtin a Docker container. 另外,应使用pm2与Docker容器一起执行该应用程序。 Everything works fine until I start the container, which gives an error while trying to read the Api.yaml . 在启动容器之前,一切正常,在尝试读取Api.yaml时出现错误。

2. Steps: 2.步骤:

First, I create the project: 首先,我创建项目:

yo express-no-stress server
cd server
npm i

For the server I change localhost to 0.0.0.0 ( http.createServer(app).listen(port, '0.0.0.0', welcome(port)); ), run the server with npm run dev and do curl 0.0.0.0:3000/api/v1/spec , which returns - as expected - the specification of the Swagger-file. 对于服务器,我将localhost更改为0.0.0.0http.createServer(app).listen(port, '0.0.0.0', welcome(port)); ),使用npm run dev运行服务器并执行curl 0.0.0.0:3000/api/v1/spec ,它按预期返回Swagger文件的规范。

Next, I add a configuration file for pm2 to the project, pm2.json : 接下来,我将pm2的配置文件添加到项目pm2.json

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

This time, I use pm2 to start the server: 这次,我使用pm2启动服务器:

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

Again, the specification is returned. 同样,返回规范。

Next, I add a Dockerfile : 接下来,我添加一个Dockerfile

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"]

I build the image and run the container: 我建立映像并运行容器:

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

3. Problem and question 3.问题与疑问

And here is my problem: 这是我的问题:

[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'

I do not know, why the error appears and how to fix it. 我不知道为什么会出现错误以及如何解决。 I have no clue, why the path ///server/common/swagger/Api.yaml looks a bit strange and I do not even know if this causes the error. 我不知道,为什么路径///server/common/swagger/Api.yaml看起来有些奇怪,我什至不知道这是否会导致错误。 Though, the container keeps running. 虽然,容器继续运行。 So: How can I fix it, or any idea where to look at? 所以:我该如何解决它,或者在哪里有任何想法?

4. Further steps 4.进一步的步骤

However, I have done the following steps to examine the error: 但是,我已经按照以下步骤检查了错误:

curl 0.0.0.0:3000/api/v1/spec from my host machine returns: 我主机上的curl 0.0.0.0:3000/api/v1/spec返回:

<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>

Entering the container with docker exec -it aacfcc6c5744 sh : 使用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           |   
└───────────────────┴────────────────────────────────────┘

And: 和:

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

Sorry for the long question, but I wanted to make sure that all my steps are hopefully clear and reproducible. 很长的问题,很抱歉,但是我想确保我的所有步骤都希望清楚且可重复。 Let my know, if you need more information. 让我知道,如果您需要更多信息。

5. Edit : 5.编辑

The problem has something to do with pm2. 问题与pm2有关。 If I enter the container, change the port of the application to 3001 (since 3000 is already in use) and do npm start ( cd build && node main ) it works and I can access the specification from my local machine. 如果我进入容器,将应用程序的端口更改为3001(因为已经在使用3000),并执行npm startcd build && node main ),并且可以从本地计算机访问规范。

Problem is solved. 问题解决了。 I just needed to add the current working directory with cwd to the configuration file for pm2: 我只需要将带有cwd的当前工作目录添加到cwd的配置文件中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM