在 Docker 中的 运行 yarn tsc 之后找不到脚本错误

Script not found error after running yarn tsc in Docker

我用 docker 运行 yarn tsc 构建了我的项目。当我运行docker run -p88:5011 accounts2

我收到错误 PM2 error: Error: Script not found: /home/accounts/dist/server/index.js 当然,如果我手动执行该过程,就会出现错误。

这是我的 docker 文件

FROM node:10.16-alpine
# Install PM2
RUN npm install pm2 -g
# Add bash support to image
RUN apk add --no-cache bash
# Create service directory
RUN mkdir -p /home/accounts

WORKDIR /home/accounts
# Copy working files
COPY package.json /home/accounts
COPY . /home/accounts
# Install dependencies
RUN yarn install
RUN yarn tsc
EXPOSE 5011
# Start command
CMD [ "pm2-runtime", "process.yml" ]

我的process.yml

apps:

  - script: ./dist/server/index.js
    name: accounts
    instances: maad
    exec_mode: cluster
    watch: false
    autorestart: true
    out_file: /dev/null
    ignore_watch:
        - logs
        - node_modules
        - worker\.js
        - \.git/index\.lock
        - \.log
        - \.lock
    watch_options:
        followSymlinks: false

目录结构

home/
 .circleci/
  config.yml
 src/
 dist/
  server/
   index.js
 process.yml

您正在将应用程序复制到 /home/accounts,它不是工作目录,应该是

WORKDIR /home/accounts-service
COPY . .

或者您将 WORKDIR 设置为

WORKDIR /home/accounts

当容器启动时 pm2-runtime 查看当前工作目录,因为您在 process.yml.

中使用相对路径
script  (string)    ”./api/app.js”  script path relative to pm2 start

pm2-application-declaration