docker容器内安装pm2

Dockerfile

ARG NODE6_VERSION
FROM ${NODE6_VERSION}

ARG TZ
ARG NODE6_VERSION
ARG CONTAINER_PACKAGE_URL

# 修改镜像源
RUN if [ "${CONTAINER_PACKAGE_URL}" != "" ]; then \
        sed -i "s/dl-cdn.alpinelinux.org/${CONTAINER_PACKAGE_URL}/g" /etc/apk/repositories; \
    fi

# 解决could not get uid/gid问题,https://www.jianshu.com/p/d460ec3c693f
RUN npm config set unsafe-perm true
# 安装指定版本,解决与node和npm的版本兼容问题
RUN npm install pm2@3.2.8 -g

WORKDIR /www

.env文件

#
# Node 6
#
NODE_VERSION=6.11.3-alpine

#
# 时区
#
TZ=Asia/Shanghai

#
# 镜像源地址
#
# 可以为空,可选项如下:
# mirrors.163.com
# mirrors.aliyun.com
# mirrors.ustc.edu.cn
#
CONTAINER_PACKAGE_URL=mirrors.aliyun.com

docker-compose.yml

node6:
    build:
      context: ./node6
      args:
        NODE6_VERSION: node:${NODE_VERSION}
        CONTAINER_PACKAGE_URL: ${CONTAINER_PACKAGE_URL}
        TZ: "$TZ"
    container_name: node6
    deploy:
      resources:
        limits:
          cpus: '0.10'
          memory: 2G
    environment:
      - NODE_ENV=production
    volumes:
      - ${SOURCE_DIR}:/www/:rw
    expose:
      - "3000"
    stdin_open: true
    tty: true
    # 容器内使用pm2-runtime,而不是pm2,参考https://stackoverflow.com/questions/55936473/docker-exits-with-code-0-when-using-pm2-start
    command: pm2-runtime start npm --name 'fun' -- run start
    networks:
      my_net:
        ipv4_address: 172.18.0.8

pm2启动命令pm2-runtime

这里注意容器内使用pm2-runtime,不能用pm2,否则报

Docker exits with code 0 when using pm2 start

原因在于pm2是后台运行,docker容器不知道pm2应用的启动,使用pm2-runtime可以进行前端运行,请查看stackoverflow

参考

posted @ 2022-02-22 15:31  whyly  阅读(1898)  评论(0编辑  收藏  举报