在docker中使用pm2运行Node.js/Swagger应用程序:打开Api.yaml时出错
1。总结: 我已经创建了一个简单的应用程序,它使用Node.js、Express和Swagger,方法如下并使用。此外,应用程序应在Docker容器中使用wihtin执行。在我启动容器之前,一切都正常,在尝试读取Api.yaml时出现了一个错误 2。步骤: 首先,我创建项目:在docker中使用pm2运行Node.js/Swagger应用程序:打开Api.yaml时出错,node.js,express,docker,swagger,pm2,Node.js,Express,Docker,Swagger,Pm2,1。总结: 我已经创建了一个简单的应用程序,它使用Node.js、Express和Swagger,方法如下并使用。此外,应用程序应在Docker容器中使用wihtin执行。在我启动容器之前,一切都正常,在尝试读取Api.yaml时出现了一个错误 2。步骤: 首先,我创建项目: yo express-no-stress server cd server npm i 对于服务器,我将localhost更改为0.0.0.0(http.createServer(app).listen(port,'0.
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
,如预期的那样返回Swagger文件的规范
接下来,我将pm2的配置文件pm2.json添加到项目中:
这次,我使用pm2启动服务器:
$ pm2 start pm2.json
$ curl 0.0.0.0:3000/api/v1/spec
$ pm2 delete all
再次返回规范
接下来,我添加一个Dockerfile:
我构建映像并运行容器:
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
看起来有点奇怪,我甚至不知道这是否会导致错误。尽管如此,容器仍在运行。那么:我如何修复它,或者知道在哪里可以看到它
4。进一步的步骤
但是,我已经完成了以下步骤来检查错误:
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>
以及:
很抱歉问了这么长的问题,但我想确保我的所有步骤都清晰且可重复。如果你需要更多信息,请告诉我
5。编辑:
这个问题与pm2有关。如果我进入容器,将应用程序的端口更改为3001(因为3000已经在使用中),然后执行
npm start
(cd build&&node main
)它工作,我可以从本地机器访问规范。问题已解决。我只需要将带有cwd
的当前工作目录添加到pm2的配置文件中:
[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'
<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>
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
{
"name": "server",
"script": "build/main.js",
"cwd": "build/",
"env": {
"APP_ID": "server",
"PORT": "3000",
"LOG_LEVEL": "debug",
"REQUEST_LIMIT": "100kb",
"SESSION_SECRET": "mySecret"
}
}