已停止 Docker 容器的资源使用情况

Resource Usage by stopped Docker containers

Docker 可以轻松停止和重启容器。它还具有暂停和取消暂停容器的能力。 Docker 文档状态

When the container is exited, the state of the file system and its exit value is preserved. You can start, stop, and restart a container. The processes restart from scratch (their memory state is not preserved in a container), but the file system is just as it was when the container was stopped.

我通过设置一个运行 memcached 的容器来测试这一点,向 memcache 写入一个值,然后

文档中的某个地方 - 我再也找不到准确的文档 - 我读到停止的容器不会消耗 CPU 或内存。然而:

非常感谢任何能够澄清这些问题的人。

我不是 docker 核心方面的专家,但我会尝试回答其中的一些问题。

  1. I suppose the fact that the file system state is preserved means that the container still does consume some space on the host's file system?

是的。 Docker保存/var/lib/docker中的所有容器和镜像数据。保存容器和图像数据的默认方式是使用 aufs。每一层的数据保存在/var/lib/docker/aufs/diff下。创建新容器时,还会创建一个新层,其中包含文件夹,其中存储了源图像层的更改。

  1. Is there a performance hit (other than host disk space consumption) associated with having 10s, or even 100s, of stopped containers in the system? For instance, does it make it any harder for Docker to startup and manage new containers?

据我所知,应该不会影响性能。当您停止容器时,docker 守护进程会向该容器的所有进程发送 SIGTERM 和 SIGKILL,如 docker CLI documentation:

中所述

Usage: docker stop [OPTIONS] CONTAINER [CONTAINER...]

Stop a running container by sending SIGTERM and then SIGKILL after a grace period

-t, --time=10 Number of seconds to wait for the container to stop before killing it. Default is 10 seconds.


3.And finally, if Paused containers retain their memory state when Unpaused - as demonstrated by their ability to remember memcached keys - do they have a different impact on CPU and memory?

正如@Usman 所说,docker 使用 cgroup freezer 实现 pause/unpause。如果我没记错的话,当你将一个进程放入冷冻机(或其 cgroup)时,你会阻止内核任务调度程序执行该进程的新任务(即:它会停止进程),但你不会杀死它们,它们会继续消耗正在使用的内存(尽管内核可能会将内存移动到交换或固态磁盘)。我认为暂停容器使用的 CPU 资源微不足道。有关此的更多信息,我会检查此功能的拉取请求,Docker issue #5948