首页 > 解决方案 > Docker - 为什么运行 ubuntu 的 50 个容器占用与 50x alpine 类似的 RAM?

问题描述

alpine 的图像大小约为 5MB,而 ubuntu - 65MB。那么为什么 RAM 使用率没有明显差异呢?如果我理解正确的话,X 的 docker 镜像会加载到内存中,然后由所有容器共享(从同一个镜像运行)。然后对于每个容器都需要创建一个单独的文件系统等,这就是为什么在这两种情况下内存使用量相似的原因。

我的理解正确吗?

$ cat script.sh 
#!/bin/bash

for i in {1..50}
do
    docker container run alpine sleep 60 &
done

$ free -mh
              total        used        free      shared  buff/cache   available
Mem:          7.8Gi       1.8Gi       4.5Gi       119Mi       1.4Gi       5.6Gi
Swap:         2.0Gi       103Mi       1.9Gi

$ ./script.sh 
$ free -mh

              total        used        free      shared  buff/cache   available
Mem:          7.8Gi       3.4Gi       2.9Gi       122Mi       1.5Gi       4.0Gi
Swap:         2.0Gi       103Mi       1.9Gi

$ vim script.sh 
$ free -mh
              total        used        free      shared  buff/cache   available
Mem:          7.8Gi       1.9Gi       4.5Gi       119Mi       1.4Gi       5.6Gi
Swap:         2.0Gi       103Mi       1.9Gi

$ cat script.sh 
#!/bin/bash

for i in {1..50}
do
    docker container run ubuntu sleep 60 &
done

$ ./script.sh 
$ free -mh
              total        used        free      shared  buff/cache   available
Mem:          7.8Gi       3.4Gi       2.9Gi       122Mi       1.5Gi       4.0Gi
Swap:         2.0Gi       103Mi       1.9Gi

标签: docker

解决方案


推荐阅读