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

时间:2018-12-18 21:12:32

标签: node.js docker docker-compose pm2

我正在尝试在具有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

我尝试分别在-g处分别添加标志--unsafe-permRUN npm install pm2。我还尝试在每个USER node中添加Dockerfile,但也没有用。

我的文件结构如下:

.
├── 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:事实证明,我必须在-g中添加RUN npm install pm2标志。然后我遇到了另一个错误,那就是容器找不到express。我通过从本地计算机(容器外部)运行npm install来解决它,并且卷配置设法将node_modules文件夹同步到容器中。

1 个答案:

答案 0 :(得分:0)

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

所有这些之后,NGINX能够提供该页面,并且现在它已启动并正在运行。 :)