DaNeSh
DaNeSh

Reputation: 1062

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.

But there is no way for me restart the Node server without restarting the whole docker container.

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.

I'm already using shared folder to have the latest code in my container.

Upvotes: 6

Views: 16758

Answers (3)

Jay
Jay

Reputation: 323

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 This will work even if you don't have nodemon installed in the image.

e.g.

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

Upvotes: 3

Nick Bernard
Nick Bernard

Reputation: 780

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. To leverage these, you'd need to update the command in your Dockerfile, e.g. 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.

Upvotes: 18

Kevin Wittek
Kevin Wittek

Reputation: 1592

I think that's not the optimal way to Docker. You should try to build your own Docker image which includes your code changes. 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.

Upvotes: -6

Related Questions