简体   繁体   English

如何在不重启整个容器的情况下在 docker 容器上重启 Node?

[英]How to restart Node on a docker container without restarting the whole container?

I have a container ==> FROM node:5 Node should restart after each change in the code.我有一个容器 ==> FROM node:5每次更改代码后节点都应该重新启动。

But there is no way for me restart the Node server without restarting the whole docker container.但是我没有办法在不重启整个 docker 容器的情况下重启 Node 服务器。

I have many npm install on dockerfile that runs each time I restart the container, and it's annoying to wait for all of them after each change in the code.我在每次重新启动容器时都会在 dockerfile 上npm install许多npm install ,并且在每次更改代码后等待所有这些npm install很烦人。

I'm already using shared folder to have the latest code in my container.我已经在使用共享文件夹在我的容器中保存最新代码。

I think that's not the optimal way to Docker. 我认为这并不是使用Docker的最佳方法。 You should try to build your own Docker image which includes your code changes. 您应该尝试构建自己的Docker映像,其中包括您的代码更改。 In your own Docker image you could make the npm install step part of the container build so you do not need to run this command on container startup. 在您自己的Docker映像中,您可以将npm install步骤作为容器构建的一部分,因此您不需要在容器启动时运行此命令。

If you just need the Node.js server to restart after changes, instead of starting the server with the node command you can use a tool like node-dev or nodemon . 如果您只需要在更改后重启Node.js服务器,而不是使用node命令启动服务器,则可以使用诸如node-devnodemon之类的工具。 To leverage these, you'd need to update the command in your Dockerfile, eg CMD [ "node-dev", "server.js" ] . 要利用它们,您需要更新Dockerfile中的命令,例如CMD [ "node-dev", "server.js" ]

Since you already have your local files as a volume in the Docker container, any change to those files will restart the server. 由于您已经将本地文件作为卷存储在Docker容器中,因此对这些文件的任何更改都将重新启动服务器。

Here's how I did it.这是我如何做到的。 In your docker-compose.yml, in the entry for the appropriate service, add an entrypoint to run npx nodemon /path/to/your/app在您的 docker-compose.yml 中,在相应服务的条目中,添加一个入口点以运行 npx nodemon /path/to/your/app

eg例如

services:
  web:
    image: foo
    entrypoint: ["npx", "nodemon", "/usr/src/app/app.js"]

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

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