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:
docker stop [container]
Is this the proper way?
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.
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
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
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With