docker容器杀不死_了解如何停止,杀死和清理Docker容器

docker容器杀不死

In the previous article ‘learn how to create and start Docker containers’, we discussed the docker run, create and start commands. We discussed how to customize the properties of a container. We will build on the concepts covered in that tutorial in this tutorial. This tutorial assumes you have a good understanding of the main parts of Docker, you have set up Docker and you have some knowledge of images and containers.

在上一篇文章“ 学习如何创建和启动Docker容器 ”中,我们讨论了Docker运行,创建和启动命令。 我们讨论了如何自定义容器的属性。 在本教程中,我们将以该教程中涵盖的概念为基础。 本教程假定您对Docker的主要部分有充分的了解,已经设置了Docker并且对图像和容器有一定的了解。

Starting and stopping containers is different from stopping and resuming ordinary processes. A process stop does not pause the process, it causes the process to exit. A stopped container is not returned by docker ps. To stop a container you use the docker stop command and pass the name of the container and the number of seconds before a container is killed. The default number of seconds the command will wait before the killing is 10 seconds.Before we demonstrate how to kill a container, let us check if there are containers running. The command below will do that.

启动和停止容器不同于停止和恢复普通过程。 进程停止不会暂停进程,而是导致进程退出。 docker ps不返回已停止的容器。 要停止容器,请使用docker stop命令,并传递容器的名称以及杀死容器之前的秒数。 在杀死之前,命令等待的默认秒数为10秒。在演示如何杀死容器之前,让我们检查是否有容器在运行。 下面的命令将执行此操作。

sudo docker ps
Image for post

The docker ps command returns a list of all running containers, from our output above we do not have any running container. In the ‘learn how to use images’ tutorial, we demonstrated how to search and download images from the Docker hub. We downloaded a WordPress image that we can use to create a container. Let us start a container using the WordPress image with the command below.

docker ps命令返回所有正在运行的容器的列表,从上面的输出中我们没有任何正在运行的容器。 在“学习如何使用映像”教程中,我们演示了如何从Docker集线器中搜索和下载映像。 我们下载了可用于创建容器的WordPress图像。 让我们使用带有以下命令的WordPress图像启动一个容器。

sudo docker create --name firstkill bitnami/wordpress
Image for post

After starting the container use sudo docker ps -a command to return all containers.

启动容器后,使用sudo docker ps -a命令返回所有容器。

Image for post

From the output above, we can see the container we have created. Something to note about docker is that all containers that were running at shut down will be restarted on a reboot.

从上面的输出中,我们可以看到我们创建的容器。 关于Docker的注意事项是,所有在关闭状态下运行的容器将在重新启动后重新启动。

We use the run command to run our container by passing the ID returned by docker ps -a to run as shown below.

我们使用run命令通过传递docker ps -a返回的ID来运行容器,如下所示。

sudo docker run fe1f71042611

Use docker ps to return only the containers that are running.

使用docker ps仅返回正在运行的容器。

Image for post

From the output above our container shows its status as exited. When a container exits with code 0 there are no errors.

从上面的输出中,我们的容器将其状态显示为退出。 当容器以代码0退出时,没有错误。

Because containers are just like any other Unix process we can interact with them via Unix signals. For example, specifying the number of seconds before a container is killed is an example of a signal. A normal process exit receives a SIGTERM signal. When a SIGTERM signal is sent but a process does not exist within the specified time, a SIGKILL signal is sent.

因为容器与任何其他Unix进程一样,我们可以通过Unix信号与它们进行交互。 例如,指定杀死容器之前的秒数是信号的示例。 正常流程出口会收到SIGTERM信号。 发送SIGTERM信号但在指定时间内不存在进程时,将发送SIGKILL信号。

When stopping a container is not possible you have to kill it. To kill a container you use docker kill command and pass the container ID. You can pass any Unix signal when calling the kill command. When you kill a container you can start it again just like you start a container that was properly stopped.

如果无法停止容器,则必须将其杀死。 要杀死容器,请使用docker kill命令并传递容器ID。 调用kill命令时,可以传递任何Unix信号。 当您杀死容器时,可以像重新启动已正确停止的容器一样重新启动它。

When you do not need a container momentarily to perform administrative functions, it is convenient to pause other than stop containers. Pausing containers rely on cgroups freezer which blocks the scheduling of frozen containers.

当您暂时不需要容器来执行管理功能时,除了停止容器之外,还可以方便地暂停。 暂停容器依赖于cgroups冻结器,该冻结器阻止冻结容器的调度。

A key difference between pausing and stopping containers is in the persistence of state. When a container is stopped any resources allocated to it such as memory are released while a paused container does not release its allocated resources.

暂停和停止容器之间的主要区别在于状态的持久性。 当容器停止时,分配给它的所有资源(例如内存)都会释放,而暂停的容器不会释放其分配的资源。

The commands below start our firstkill container, pause it and check its status. Use the container ID returned by sudo docker ps -a.

下面的命令启动我们的firstkill容器,暂停它并检查其状态。 使用sudo docker ps -a返回的容器ID。

sudo docker run fe1f71042611sudo docker pause fe1f71042611sudo docker ps -a
Image for post

The actions of creating and manipulating containers result in many image layers and container-specific folders. Cleaning up these resources is important. In the next section, we look at how you can remove images and containers.

创建和操作容器的操作会产生许多图像层和特定于容器的文件夹。 清理这些资源很重要。 在下一节中,我们将介绍如何删除图像和容器。

To remove a container pass the container ID to the docker rm command.

要删除容器,请将容器ID传递给docker rm命令。

The docker images command returns a list of all images on your host. To delete an image pass the ID returned by docker images to docker rmi command. An example of deleting an image is shown below.

docker images命令返回主机上所有图像的列表。 要删除映像,请将docker images返回的ID传递给docker rmi命令。 删除图像的示例如下所示。

sudo docker images
sudo docker rmi

Our attempt to remove the helloworld image above failed because there is a running container based on the image. We should always remove a container before we can remove an image. The error gives you the ID of the container making it very simple to remove the container. Let us remove the container, then remove the image.

我们尝试删除上面的helloworld映像的尝试失败,因为存在基于该映像的正在运行的容器。 在删除图像之前,应始终删除容器。 该错误为您提供了容器的ID,从而使删除容器变得非常简单。 让我们删除容器,然后删除图像。

Image for post

In this tutorial, we discussed the difference between normal processes and docker containers. We discussed how to stop a running container. We also discussed how to kill a running container. We noted the difference between stopping and killing a container. We discussed how to pause and resume a container. Finally, we discussed how to remove images and containers.

在本教程中,我们讨论了正常进程和Docker容器之间的区别。 我们讨论了如何停止正在运行的容器。 我们还讨论了如何杀死正在运行的容器。 我们注意到停止和杀死容器之间的区别。 我们讨论了如何暂停和恢复容器。 最后,我们讨论了如何删除图像和容器。

WE HAVE ALSO CREATED AN EXCLUSIVE ARTICLE ON “BEST C & C++ IDES & EDITORS” FOR PROGRAMMERS TO ENHANCE THE EFFICIENCY & PRODUCTIVITY. THIS ARTICLE ALSO INCLUDES SOME COOL INFOGRAPHICS ON C & C++ PROGRAMMING! DO READ IT!

我们还为程序员提供了有关“ 最佳C&C ++ IDE和编辑器 ”的专论,以提高效率和生产率。 该文章还包括有关C和C ++编程的一些凉爽信息! 阅读它!

Originally published at https://blog.eduonix.com on February 13, 2020

最初于 2020 2月13日发布在 https://blog.eduonix.com

翻译自: https://medium.com/eduonix/learn-how-to-stop-kill-and-clean-up-docker-containers-2fd311c0c384

docker容器杀不死

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值