无法在 docker 容器(Vultr 主机)中全局安装 pm2

Can't install pm2 globally inside a docker container (Vultr host)

我正在尝试在 Ubuntu 18.04 的 Vultr 服务器中使用 docker-compose。我创建了以下 Dockerfile:

FROM node:latest

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./

RUN npm install -g pm2
RUN npm install
# If you are building your code for production
# RUN npm install --only=production

# Bundle app source
COPY . .

EXPOSE 80
CMD [ "pm2-runtime", "server.js", "--watch" ]

docker-compose.yml 文件是:

version: '3'

services:
  nginx:
    image: nginx:latest
    volumes:
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
    ports:
      - 80:80
    networks:
      - backbone
  web:
    build: ./../services/web
    volumes:
      - ./../services/web:/usr/src/app
    networks:
      - backbone
  edu_entities:
    build: ./../services/edu_entities
    volumes:
      - ./../services/edu_entities:/usr/src/app
    networks:
      - backbone
networks:
  backbone:
    driver: bridge

我得到的输出如下:

root@vultr-host:~/app/config# docker-compose build
nginx uses an image, skipping
Building edu_entities
Step 1/8 : FROM node:latest
 ---> 37f455de4837
Step 2/8 : WORKDIR /usr/src/app
 ---> Using cache
 ---> 282a6fa7bf8a
Step 3/8 : COPY package*.json ./
 ---> Using cache
 ---> 8b6970aecfec
Step 4/8 : RUN npm install pm2
 ---> Running in 2449fb50b303
npm WARN edu_entities@1.0.0 No description
npm WARN edu_entities@1.0.0 No repository field.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.4 (node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.4: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

+ pm2@3.2.3
added 404 packages from 315 contributors and audited 2675 packages in 28.626s
found 0 vulnerabilities

Removing intermediate container 2449fb50b303
 ---> 5c33d7153e3c
Step 5/8 : RUN npm install
 ---> Running in 0f0a26e6ad00
audited 2675 packages in 5.301s
found 0 vulnerabilities

npm WARN edu_entities@1.0.0 No description
npm WARN edu_entities@1.0.0 No repository field.                                                                      
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.4 (node_modules/fsevents):                               
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.4: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})                                                                           

Removing intermediate container 0f0a26e6ad00
 ---> ff9817b28907
Step 6/8 : COPY . .
 ---> 373ed677369f
Step 7/8 : EXPOSE 80
 ---> Running in bf44032ac43e
Removing intermediate container bf44032ac43e
 ---> 60ed79bedc2a
Step 8/8 : CMD [ "pm2-runtime", "server.js", "--watch" ]
 ---> Running in 9487a4f0fc5d
Removing intermediate container 9487a4f0fc5d
 ---> cdac633adfb8
Successfully built cdac633adfb8
Successfully tagged config_edu_entities:latest
Building web
Step 1/8 : FROM node:latest
 ---> 37f455de4837
Step 2/8 : WORKDIR /usr/src/app
 ---> Using cache
 ---> 282a6fa7bf8a
Step 3/8 : COPY package*.json ./
 ---> 8943e5aeb3c4
Step 4/8 : RUN npm install pm2
 ---> Running in 4dc7c76fb478

然后它会在这里停留很长时间...

Killed
ERROR: Service 'web' failed to build: The command '/bin/sh -c npm install pm2' returned a non-zero code: 137

我尝试在 RUN npm install pm2 处单独或单独添加标志 -g--unsafe-perm。我还尝试在每个 Dockerfile 中添加 USER node,但它也没有用。

我的文件结构如下:

.
├── config
│   ├── bin
│   │   └── ...
│   ├── docker-compose.yml
│   └── nginx
│       └── default.conf
└── services
    ├── edu_entities
    │   ├── Dockerfile
    │   ├── package-lock.json
    │   ├── package.json
    │   ├── server.js
    │   └── src
    │       └── ...
    └── web
        ├── Dockerfile
        ├── package-lock.json
        ├── package.json
        ├── server.js
        └── src
            └── ...

编辑: 正如@codestation 在评论中所说,可能是主机 VM 中缺少可用 RAM。今天早上我尝试了 运行 docker-compose build 并且成功了。但是,现在出现以下错误:

root@vultr-host:~/app/config# docker-compose up
Starting config_nginx_1 ... 
Starting config_web_1 ... 
Starting config_nginx_1
Starting config_web_1 ... error

ERROR: for config_web_1  Cannot start service web: OCI runtime create failed: container_linux.go:348: starting contain
Starting config_nginx_1 ... done

ERROR: for web  Cannot start service web: OCI runtime create failed: container_linux.go:348: starting container process caused "exec: \"pm2-runtime\": executable file not found in $PATH": unknown
ERROR: Encountered errors while bringing up the project.

编辑 2: 结果我必须在 RUN npm install pm2 中添加 -g 标志。然后又遇到了一个错误,就是容器找不到express。我通过 运行 npm install 从本地机器(在容器外)解决了它,并且卷配置设法将 node_modules 文件夹同步到容器中。

最初的错误是 Vultr 主机 VM 没有足够的可用 RAM,因此 docker-compose build 在必须安装 pm2 时失败了。然后,我还必须在我的服务 DockerfileRUN npm install pm2 中添加 -g 标志。我还必须 运行 npm install 在容器外部添加其他依赖项并让卷将它们放入容器中。

完成这一切后,NGINX 能够提供该页面,现在它已经启动 运行ning。 :)