pzaenger
pzaenger

Reputation: 11994

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

1. Summary:

I have created a simple application, which uses Node.js, Express and Swagger, by following this tutorial and using generator-express-no-stress. In addition the application should be executed with pm2 wihtin a Docker container. Everything works fine until I start the container, which gives an error while trying to read the Api.yaml.

2. Steps:

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.

Next, I add a configuration file for pm2 to the project, 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 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:

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

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. Though, the container keeps running. So: How can I fix it, or any idea where to look at?

4. Further steps

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:

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

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:

The problem has something to do with 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.

Upvotes: 4

Views: 1580

Answers (1)

pzaenger
pzaenger

Reputation: 11994

Problem is solved. I just needed to add the current working directory with cwd to the configuration file for pm2:

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

Upvotes: 0

Related Questions