How to restart Node on a docker container without restarting the whole container?
Asked Answered
C

3

6

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.

Commissar answered 19/5, 2016 at 16:17 Comment(4)
Possible duplicate of Docker node development environment on windowsHillary
@JarrodRoberson I'm not asking about copy/share source code. I already used share folders to have the latest source code in docker!Commissar
The packages only get installed when you perform docker build. When u start the container the packages do not get reinstalled.Bristow
for future reference: I was installing all the npm packages in the entrypoint which is wrong. That was the reason the restart was taking too long. installing packages should be in the Dockerfile RUN commands itself.Commissar
A
-6

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.

Agnusago answered 19/5, 2016 at 16:24 Comment(0)
R
18

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.

Ragg answered 26/11, 2019 at 0:33 Comment(1)
This is the actual answer to the question.Twocycle
T
1

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"]
Tolliver answered 30/12, 2021 at 22:0 Comment(0)
A
-6

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.

Agnusago answered 19/5, 2016 at 16:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.