简体   繁体   English

如何停止在docker中运行节点

[英]How to stop running node in 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. Control-C 没有做任何事情。

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.搜索所有正在运行的 docker 容器。
  4. Then docker stop [container]然后docker stop [container]

Is this the proper way?这是正确的方法吗?

As described here: github: docker-node best practice如此处所述: github:docker-node 最佳实践

You can add the --init flag to your docker run command.您可以将--init标志添加到您的 docker run 命令中。

docker run -it --init -p 3000:3000 --name nodetest mynodeimage

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).但正确的方法是捕获 SIGINT(javascript 中的中断信号)。

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

This should do the trick when you press Ctrl+C当您按 Ctrl+C 时,这应该可以解决问题

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:我发现(通过反复试验)只有当 Dockerfile 中的 CMD 设置为:

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

However, Ctrl+C works fine when the CMD is changed to:但是,当 CMD 更改为:

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.我的package.json文件中的npm start脚本设置为node server.js ,所以我不知道为什么此更改有效,但希望这会有所帮助。

Adocker run should have gave you back the prompt, avoiding the need for CTRL+C, or closing the docker terminal. docker run应该会返回提示,避免使用 CTRL+C 或关闭 docker 终端。

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)重新登录该终端后, docker ps -a + docker stop应该足以使您的容器退出(您仍然需要在尝试再次启动之前将其删除)

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>
  1. docker stop <containerName/containerId>泊坞窗停止<containerName/containerId>
  2. docker kill --signal=SIGINT <containerName/containerId> docker kill --signal=SIGINT <containerName/containerId>
  3. docker rm -f <containerName/containerId> docker rm -f <containerName/containerId>

As a part of solution, you can open your package.js and add 3 new commands/scripts :作为解决方案的一部分,您可以打开 package.js 并添加 3 个新命令/脚本:

"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.在 docker 的 3001 端口上启动您的应用程序并玩得开心。 Every next run will clear previous and build/run again.每次下一次运行都会清除上一次并再次构建/运行。

To stop and delete it, just run :要停止并删除它,只需运行:

npm run docker-stop-and-clear

From what I can gather you need both -t and -i for Ctrl-C to work as expected.据我所知,您需要-t-i才能使 Ctrl-C 按预期工作。 Command like this would be helpful i believe.我相信这样的命令会有所帮助。

Simple example which i can think of this below Case 1 to retain container:简单的例子,我可以在案例 1 下想到这个来保留容器:

$ 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:案例2终止容器:

$ ID=$(sudo docker run -t -i -d ubuntu /usr/bin/top -b)
$ sudo docker attach $ID
Control-C
$ sudo docker ps

Inside the node console, after running docker run -it node , you can exit with the following:在节点控制台中,运行docker run -it node后,您可以使用以下命令退出:

  • Enter .exit输入.exit
  • Press two times按两次
    • ctrl+c Ctrl+C
    • ctrl+d Ctrl+d

If the node container is started in detached mode docker run -d node , you can stop it with docker stop <CONTAINER_ID or CONTAINER_NAME> .如果节点容器以分离模式启动docker run -d node ,您可以使用docker stop <CONTAINER_ID or CONTAINER_NAME>停止它。

For example, assuming you want to kill the newest node container:例如,假设你想杀死最新的节点容器:

docker stop $(docker ps | grep node | awk 'NR == 1 { print $1}')

The solution is to use in the Dockerfile this command for starting the application解决方案是在Dockerfile中使用这个命令来启动应用程序

CMD ["/bin/sh", "-c", "node app.js"]

then we can listen in the app.js with然后我们可以在app.js中监听

process.on('SIGTERM', () => {
  console.log('SIGTERM received, shutting down...');
});

and we have to run the dockerfile with the --init flag我们必须使用--init标志运行 dockerfile

docker run --init -p 3000:3000 --name nodetest mynodeimage

or we can add in docker-compose beginning from version 3.7 the entry或者我们可以从3.7版本开始在docker-compose中添加条目

init: true

to the desired service.到所需的服务。 For the app to receive the signal you should use docker stop nodetest or docker-compose down .要让应用程序接收信号,您应该使用docker stop nodetest或 docker docker-compose down Shutting down with Ctrl+C does not send the SIGTERM signal.使用Ctrl+C关闭不会发送SIGTERM信号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM