杀死正在运行的Docker容器后进行清理 [英] Cleaning up after killing a running docker container

查看:384
本文介绍了杀死正在运行的Docker容器后进行清理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是编写一个运行python脚本的docker映像,该脚本会生成很多充满随机数的csv文件,一旦完成,这些文件将被写入外部存储驱动器,然后容器退出.假设它写入了很多这样的csv文件,以致它们无法存储到内存中.

My goal is to write a docker image that runs a python script that produces a lot of csv files full of random numbers, which once finished, are to be written to an external storage drive, after which the container quits. Assume that it writes so many of these csv files that they cannot be stored into memory.

我担心的是容器遇到错误并退出(或由用户退出),然后创建了一堆必须手动清理的垃圾文件的情况.

What I'm worried about are the cases where the container encounters an error and quits (or is made to quit by the user), and then it creates a bunch of garbage files that have to be manually cleaned.

第一个解决方案是将快速驱动器(如SSD)直接安装到容器中并对其进行写入.完成后,它将数据从此SSD传输到外部存储驱动器.一件不好的事情是,如果容器意外退出,它将在SSD上留下垃圾.

First solution is to mount a fast drive (like an SSD) directly into the container and write to it. After it is done, it transfers the data from this SSD to the external storage drive. The bad thing about this one is that if the container quits unexpectedly, it will leave garbage on the SSD.

第二个解决方案是使用SSD创建一个卷,使用该卷启动一个容器,然后执行与第一个解决方案几乎相同的操作.在这种情况下,如果容器意外死亡,那么体积将如何变化?它也会自动退出吗?可以将其配置为自动退出,从而删除已创建的任何垃圾吗?

Second solution was to create a volume using the SSD, start a container with this volume, and then pretty much do the same as the first solution. In this case, if the container dies unexpectedly, what happens to the volume? Will it auto quit as well? Can it be configured to auto quit thereby deleting any garbage that was created?

如果您感到好奇,那么最终目标是将这些容器与某种编排系统一起使用.

In case you're curious, the final goal is to use these containers with some sort of orchestration system.

推荐答案

我担心的是容器遇到错误并退出(或由用户退出),然后创建了一堆必须手动清理的垃圾文件的情况.

What I'm worried about are the cases where the container encounters an error and quits (or is made to quit by the user), and then it creates a bunch of garbage files that have to be manually cleaned.

请注意,您可以将ENTRYPOINT Python脚本配置为自动执行必要的清理.

Note that you may configure your ENTRYPOINT Python script to automatically perform the necessary cleanup.

为您提供这种方法的一些指导原则/示例:

To give you some guidelines/examples of that approach:

  • 我在这个SO答案中给出了一个这样的示例(在Bash中实现,即带有trap).
  • I gave one such example (implemented in Bash, namely with a trap) in this SO answer.
  • Another possible example (implemented in Python) is given in this blog article.

请注意,除了可以正常终止容器之外,您可能还需要设置restart策略,例如alwaysunless-stopped.例如,请参见此codeship博客文章.

Note that beyond the graceful termination of your containers, you may want to setup a restart policy, such as always or unless-stopped. See for example this codeship blog article.

第一个解决方案是将快速驱动器(如SSD)直接安装到容器中并对其进行写入.完成后,它将数据从此SSD传输到外部存储驱动器.一件不好的事情是,如果容器意外退出,它将在SSD上留下垃圾.

First solution is to mount a fast drive (like an SSD) directly into the container and write to it. After it is done, it transfers the data from this SSD to the external storage drive. The bad thing about this one is that if the container quits unexpectedly, it will leave garbage on the SSD.

第二个解决方案是使用SSD创建一个卷,使用该卷启动一个容器,然后执行与第一个解决方案几乎相同的操作.在这种情况下,如果容器意外死亡,那么体积将如何变化?它也会自动退出吗?

Second solution was to create a volume using the SSD, start a container with this volume, and then pretty much do the same as the first solution. In this case, if the container dies unexpectedly, what happens to the volume? Will it auto quit as well?

尽管您提出的两个解决方案对于解决该线程的主要问题不是必需的,但我必须指出,通常,在生产中使用卷是最佳实践,而不是仅使用 bind-mount .但是,当然,使用这两个方法中的任何一个(-v volume-name:/path或绑定安装-v /path:/path)比根本不使用-v选项要好得多,因为我记得直接在容器的可写层中写入数据表示如果从映像重新创建容器,则这些数据将丢失.

Albeit the two solutions you present wouldn't be necessary to address the main question of this thread, I have to mention that in general, it is a best practice to use volumes in production, rather than using a mere bind-mount. But of course using any of these two approches (-v volume-name:/path or a bind-mount -v /path:/path) is way better than not using the -v option at all, because I recall that writing data directly in the writable layer of the container implies this data will be lost if the container is recreated from the image.

这篇关于杀死正在运行的Docker容器后进行清理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆