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 w
As a part of solution, you can open your package.js and add 3 new commands/scripts :
"scripts": {
"docker-build-and-run": "docker build -t image-dev-local . && docker run -p 3001:3001 --name container-dev-local image-dev-local",
"docker-stop-and-clear": "(docker stop container-dev-local || true) && (docker rm container-dev-local || true)",
"docker-run": "npm run docker-stop-and-clear && npm run docker-build-and-run"
}
and just simply run in the terminal :
npm run docker-run
to up your app on 3001 port in docker and have fun. Every next run will clear previous and build/run again.
To stop and delete it, just run :
npm run docker-stop-and-clear
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)
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.
From what I can gather you need both -t and -i for Ctrl-C to work as expected. Command like this would be helpful i believe.
Simple example which i can think of this below Case 1 to retain container:
$ ID=$(sudo docker run -t -d ubuntu /usr/bin/top -b)
$ sudo docker attach $ID
Control-C
$ sudo docker ps
Case 2 to terminate the container:
$ ID=$(sudo docker run -t -i -d ubuntu /usr/bin/top -b)
$ sudo docker attach $ID
Control-C
$ sudo docker ps
If you just want to stop node without stopping the container, you could go inside the container and run:
$ ps aux | grep node #to obtain process ID (value in second column)
$ kill <process ID>