【问题标题】:Can't install pm2 globally inside a docker container (Vultr host)无法在 docker 容器(Vultr 主机)内全局安装 pm2
【发布时间】:2018-12-18 21:12:32
【问题描述】:

我正在尝试在带有 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 在 cmets 中所说,可能是主机 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 文件夹同步到容器中。

【问题讨论】:

  • 内存不足了吗?有多少内存可以免费构建映像?检查 dmesg 日志。
  • 你的 Dockerfile 看起来和官方安装 pm2 github.com/keymetrics/docker-pm2/blob/master/tags/10/alpine/…的方式很相似
  • @codestation 我今天一醒来就尝试过,命令有效。正如您所说,这可能是免费内存的问题。现在出现另一个错误。我将编辑帖子以添加它。

标签: node.js docker docker-compose pm2


【解决方案1】:

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

在这一切之后,NGINX 能够为页面提供服务,现在它已经启动并运行了。 :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-08
    • 1970-01-01
    • 2014-09-17
    • 2015-11-26
    • 2020-05-07
    • 1970-01-01
    • 1970-01-01
    • 2015-03-15
    相关资源
    最近更新 更多