Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop running node in docker

Tags:

node.js

docker

I have just installed dockers and installed node. I am able to run a basic express site. My issue now is I can't stop it. Control-C is not doing anything.

Temporarily what I did to exit was:

  1. Close the docker's terminal.
  2. Open a new one.
  3. Search for all docker containers that is running.
  4. Then docker stop [container]

Is this the proper way?

like image 495
nycdanie Avatar asked Apr 15 '16 03:04

nycdanie


People also ask

How do I force docker to stop?

docker rm -f The final option for stopping a running container is to use the --force or -f flag in conjunction with the docker rm command. Typically, docker rm is used to remove an already stopped container, but the use of the -f flag will cause it to first issue a SIGKILL.


4 Answers

I don't know if this is too late. But the correct way to do this is to catch the SIGINT (interrupt signal in your javascript).

var process = require('process')
process.on('SIGINT', () => {
  console.info("Interrupted")
  process.exit(0)
})

This should do the trick when you press Ctrl+C

like image 85
Jose Mar Avatar answered Oct 08 '22 02:10

Jose Mar


As described here: github: docker-node best practice

You can add the --init flag to your docker run command.

docker run -it --init -p 3000:3000 --name nodetest mynodeimage
like image 20
Romanicus Avatar answered Oct 08 '22 02:10

Romanicus


I came across this same problem today, and struggled to find an explanation/solution. I discovered (through trial and error) that this only occurs when the CMD in the Dockerfile is set to:

CMD [ "node", "server.js" ]

However, Ctrl+C works fine when the CMD is changed to:

CMD [ "npm", "start" ]

The npm start script in my package.json file is set to node server.js, so I have no idea why this change works, but hopefully this helps.

like image 22
Alex H Avatar answered Oct 08 '22 00:10

Alex H


A docker run should have gave you back the prompt, avoiding the need for CTRL+C, or closing the docker terminal.

Once you log back in that terminal, a docker ps -a + docker stop should be enough to make your container exit (you still need to remove it before trying to launch it again)

like image 30
VonC Avatar answered Oct 08 '22 01:10

VonC