简体   繁体   English

使用pm2在Docker容器中启动两个nodejs应用程序

[英]Launch two nodejs app inside Docker container using pm2

I'm trying to launch 2 nodejs app inside a Docker container using PM2, so I made a custom Dockerfile with all the projects config 我正在尝试使用PM2在Docker容器中启动2个nodejs应用程序,所以我制作了一个包含所有项目配置的自定义Dockerfile

FROM node:argon

RUN npm install pm2 -g --silent

VOLUME ./src/app:/usr/src/app
WORKDIR /usr/src/app

RUN git clone https://github.com/yoonic/atlas.git backend
RUN cd backend && \
    npm i --silent && \
    pm2 start npm --name "backend" -- run dev --no-daemon

RUN git clone https://github.com/yoonic/nicistore.git frontend
RUN cd frontend && \
    npm i --silent && \
    sed -i "s#api.atlas.baseUrl#http://localhost:8000/v1#" config/client/development.js && \
    pm2 start npm --name "frontend" -- run dev --no-daemon

I start this container with docker-compose up with this config 我用docker-compose up这个配置开始这个容器

# NodeJS
nodejs:
  build: docker/nodejs
  container_name: nodejs
  ports:
    - 53000:3000
    - 54000:4000

When all the container is set up, I get the PM2 process list in my terminal then docker-compose start all my containers but I the nodejs one fail instantly 设置完所有容器后,我在终端中获取PM2进程列表然后docker-compose启动我的所有容器但是我的nodejs立即失败

nodejs exited with code 0 nodejs退出代码0

My nodejs app are working inside my container but this one exit instantly... 我的nodejs应用程序正在我的容器内工作,但这一个立即退出...

This the right way to make this work ? 这是使这项工作正确的方法吗? PM2 is maybe not needed ? 可能不需要PM2?

How can I make this working ? 我怎样才能使这个工作?

EDIT 编辑

The container exit when I'm not using --no-daemon because it think everything is done. 当我不使用--no-daemon时容器退出,因为它认为一切--no-daemon完成。 But when I'm using --no-daemon the build process is never finished because it show me nodejs app logs 但是当我使用--no-daemon ,构建过程永远不会完成,因为它向我显示了nodejs应用程序日志

Use a process file to manage these two applications: http://pm2.keymetrics.io/docs/usage/application-declaration/ 使用流程文件来管理这两个应用程序: http//pm2.keymetrics.io/docs/usage/application-declaration/

For example - process.yml: 例如 - process.yml:

apps:
  - script : 'npm'
    args   : 'run dev'
    cwd    : './backend'
    name   : 'backend'
  - script : 'npm'
    args   : 'run dev'
    cwd    : './frontend'
    name   : 'frontend'

Then in the Dockerfile: 然后在Dockerfile中:

CMD ['pm2-docker', 'process.yml']

Documentation about PM2/Docker integration: http://pm2.keymetrics.io/docs/usage/docker-pm2-nodejs/ 有关PM2 / Docker集成的文档: http//pm2.keymetrics.io/docs/usage/docker-pm2-nodejs/

First of all, though you can run several processes in one container usually the best way to go is to use just one process per container. 首先,虽然您可以在一个容器中运行多个进程,但最好的方法是每个容器只使用一个进程。 So you would have two services in your docker-compose.yml - one for backend and another for frontend. 所以你在docker-compose.yml有两个服务 - 一个用于后端,另一个用于前端。

There are some problems in your Dockerfile that need fixing: 您的Dockerfile中存在一些需要修复的问题:

  1. Use ADD or COPY instead of VOLUME to copy files to container 使用ADDCOPY而不是VOLUME将文件复制到容器
  2. Use RUN command just for installing npm packages etc. to prepare an image. 使用RUN命令只是为了安装npm软件包等来准备图像。
  3. Use COMMAND or ENTRYPOINT to define the command that is run when the container is started. 使用COMMANDENTRYPOINT定义启动容器时运行的命令。

So, the reason why your container is exiting is that you don't specify your own COMMAND and thus the default command from node:argon is run. 因此,您的容器退出的原因是您没有指定自己的COMMAND ,因此运行来自node:argon的默认命令。 As the default command is to start Node REPL and it exits if the container is not run in interactive mode, your container exits immediately on start-up. 由于默认命令是启动节点REPL,如果容器未以交互模式运行,它将退出,您的容器会在启动时立即退出。

I'm a bit busy now and can't prepare a full example with working code. 我现在有点忙,不能用工作代码准备一个完整的例子。 Can you find your path forward with these tips? 您能通过这些提示找到前进的道路吗? :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM