Docker磁盘使用情况

时间:2016-02-16 20:52:17

标签: docker

我正在托管一些简单的docker容器。我想知道容器的大小会随着时间的推移而迅速增加,我不知道如何找出问题。

Docker报告的大小:

me@somewhere:~$ sudo docker ps -s
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                              NAMES               SIZE
02b30add1cb3        my-service               "npm start"              23 hours ago        Up 23 hours         3001/tcp, 0.0.0.0:9017->9017/tcp   my-service-frontend      0 B (virtual 776.4 MB)
20a2be4931e7        my-service               "phantomjs src/sites/"   23 hours ago        Up 23 hours         0.0.0.0:3007->3001/tcp             my-service-5     6.144 kB (virtual 776.4 MB)
ba340ba08941        my-service               "phantomjs src/sites/"   23 hours ago        Up 23 hours         0.0.0.0:3006->3001/tcp             my-service-4     6.144 kB (virtual 776.4 MB)
7b5411d8a171        my-service               "phantomjs src/sites/"   23 hours ago        Up 23 hours         0.0.0.0:3003->3001/tcp             my-service-1     6.144 kB (virtual 776.4 MB)
b583a544b37d        my-service               "phantomjs src/sites/"   23 hours ago        Up 23 hours         0.0.0.0:3001->3001/tcp             my-service-0     6.144 kB (virtual 776.4 MB)
91373086e06e        foo_bar                  "/bin/sh -c 'git pull"   47 hours ago        Up 47 hours         0.0.0.0:12776->8080/tcp            kickass_murdock     11.26 MB (virtual 1.081 GB)

du在主持人上报告的尺寸:

me@somewhere:~$ sudo du -h -d 1 /var/lib/docker/containers
14G     /var/lib/docker/containers/20a2be4931e7a10b2e29260b541e3c4d6581462650e47d59682f84626843752b
1,6G    /var/lib/docker/containers/7b5411d8a171a35a3c937d62dbdea141fc0a9f3c4de25a2da3a0b94ea71a8f3d
9,6M    /var/lib/docker/containers/02b30add1cb3ba6d5be1c36b2c9dd141d8d70cb88a021d2363af5684ef3c220f
480K    /var/lib/docker/containers/91373086e06ea83269465e0b026cfe7ca0158a1315b0df04da9a1d1b4ee52823
13G /var/lib/docker/containers/b583a544b37db6144f17a4819ca2f636126b11d668caab3dcdbf4c3a33dedc65
13G /var/lib/docker/containers/ba340ba08941d47af45230be328ef7289c19b6bb6a0d120cf2098cbdd9983f65
40G /var/lib/docker/containers`

du为容器报告的大小(所有其他容器的类似输出):

me@somewhere:~$ sudo docker exec -it my-service-4 du -h -d1 -c /
58M /root
0   /dev
3.0M    /etc
706M    /usr
1.4M    /tmp
14M /var
9.0M    /bin
32M /lib
4.0K    /home
8.0K    /run
4.0K    /mnt
4.0K    /boot
0   /sys
4.0K    /opt
4.0K    /srv
4.0K    /lib64
3.9M    /sbin
du: cannot access '/proc/12642/task/12642/fd/3': No such file or directory
du: cannot access '/proc/12642/task/12642/fdinfo/3': No such file or directory
du: cannot access '/proc/12642/fd/3': No such file or directory
du: cannot access '/proc/12642/fdinfo/3': No such file or directory
0   /proc
4.0K    /media
825M    /
825M    total

所以:容器和docker ps都报告磁盘使用率低于1G,但实际的容器文件大小超过10 GB(至少对于某些容器而言)。任何人都可以帮助我并告诉我们发生了什么吗?我想在我的容器内发生了一些麻烦,虽然我不知道现在在哪里看。谁知道我要做什么?

1 个答案:

答案 0 :(得分:1)

只要将更改存储到图像,Docker就会在其洋葱文件系统中将一层放在另一层上。当您删除容器internal class Timer { private readonly TimeSpan _disposalTimeout; private readonly System.Threading.Timer _timer; private bool _disposeEnded; public Timer(TimeSpan disposalTimeout) { _disposalTimeout = disposalTimeout; _timer = new System.Threading.Timer(HandleTimerElapsed); } public event Signal Elapsed; public void TriggerOnceIn(TimeSpan time) { try { _timer.Change(time, Timeout.InfiniteTimeSpan); } catch (ObjectDisposedException) { // race condition with Dispose can cause trigger to be called when underlying // timer is being disposed - and a change will fail in this case. // see // https://msdn.microsoft.com/en-us/library/b97tkt95(v=vs.110).aspx#Anchor_2 if (_disposeEnded) { // we still want to throw the exception in case someone really tries // to change the timer after disposal has finished // of course there's a slight race condition here where we might not // throw even though disposal is already done. // since the offending code would most likely already be "failing" // unreliably i personally can live with increasing the // "unreliable failure" time-window slightly throw; } } } private void HandleTimerElapsed(object state) { Elapsed.SafeInvoke(); } public void Dispose() { using (var waitHandle = new ManualResetEvent(false)) { // returns false on second dispose if (_timer.Dispose(waitHandle)) { if (!waitHandle.WaitOne(_disposalTimeout)) { throw new TimeoutException( "Timeout waiting for timer to stop. (...)"); } _disposeEnded = true; } } } } 时,您应该看到/ var / lib / docker / containers占用的空间更少。

构建图像时会存储对图像的更改。如果您只使用图像 - 运行容器 - 更改的数据只是“保持”。因此,您应该调查容器中发生的情况。 ESP。看看你的幻像服务器在文件系统中产生了什么。 ncdu将是一个很好的工具。

从ncdu开始,然后存储在hosts文件系统的附加文件夹中生成的文件。 docker rmi -f CONTAINERID

<强>测试

运行一个简单的容器,只包含os(在我的情况下是alpine)

docker run -it -v FULLPATHATHOST:FULLPATHWITHINCONTAINER CONTAINERID

在主持人转到docker run -it stk/alpine:base sh 并列出包含ncdu的目录。 (当然,你可以使用你喜欢的任何其他程序来确定指令大小)

/var/lib/docker/aufs/diff/

在容器内执行类似

的操作
ncdu 1.10 ~ Use the arrow keys to navigate, press ? for help                    
--- /var/lib/docker/aufs/diff --------------------------------------------------
                        /..                                                     
  286,4MiB [##########] /39f3e2ea0dfe17366b8cd7b0...bf3681b99a1081e33ad62a509f28
  214,3MiB [#######   ] /2de39307b9361cae12f0116e...d28056e4699b21b9a4d34f374461
  207,7MiB [#######   ] /92ec6d044cb3e39ae0050012...78d0591675f2231daafbf0877778
  154,0MiB [#####     ] /9f3806e6bedc8fb01929131b...e01aa1980aadba914fdd9d2f96ae
  149,5MiB [#####     ] /5f0ca2331640639507d85b83...693659438367311abb0c792b8a62
  136,8MiB [####      ] /902b87aaaec929e805414868...1f529ad7f37ab300d4ef9f3a0dbf
  136,2MiB [####      ] /222ba86561913d299deb9e0e...6b5f5ec117b01386a4156d092687
  132,6MiB [####      ] /8b3a9a9eeaf8ed59f24f21a2...dfe8d033890a2fa44b445deb2e3c
  128,5MiB [####      ] /72b3edf317a8d682466c1500...a5e2cad31c8305ed42c41cd61149
  117,0MiB [####      ] /818e3763e72ef82b28b0552e...b9f163dc601d266e94e46fd26bb0
   57,4MiB [##        ] /eeffdfafed9f60771b5bf87a...e8bbd16b572f77899c8e689d174d
   56,1MiB [#         ] /6976ce3ed5fab37382d90467...37578332417ffcf35a1d499eba52
   51,3MiB [#         ] /a5a6e0549d247f1c8b81a350...c5071f46d17afe2f8988817360b3
 Total disk usage:   2,7GiB  Apparent size:   2,7GiB  Items: 168658                         

将创建一个名为tr -dc A-Za-z0-9 </dev/urandom | head -c 409600000 > a.txt && ls a.txt -all -h 的随机数据的文件我选择大小a.txt大于286,4MiB - 409600000中最大的文件夹 - 以便ncdu显示它在顶部

/var/lib/docker/aufs/diff/

现在我知道这是以ncdu 1.10 ~ Use the arrow keys to navigate, press ? for help --- /var/lib/docker/aufs/diff -------------------------------------------------- /.. 390,6MiB [##########] /0720a07653a57d938c861cf3...e61c81c29f12289759f0560aa38f 286,4MiB [####### ] /39f3e2ea0dfe17366b8cd7b0...bf3681b99a1081e33ad62a509f28 214,3MiB [##### ] /2de39307b9361cae12f0116e...d28056e4699b21b9a4d34f374461 207,7MiB [##### ] /92ec6d044cb3e39ae0050012...78d0591675f2231daafbf0877778 154,0MiB [### ] /9f3806e6bedc8fb01929131b...e01aa1980aadba914fdd9d2f96ae 149,5MiB [### ] /5f0ca2331640639507d85b83...693659438367311abb0c792b8a62 136,8MiB [### ] /902b87aaaec929e805414868...1f529ad7f37ab300d4ef9f3a0dbf 136,2MiB [### ] /222ba86561913d299deb9e0e...6b5f5ec117b01386a4156d092687 132,6MiB [### ] /8b3a9a9eeaf8ed59f24f21a2...dfe8d033890a2fa44b445deb2e3c 128,5MiB [### ] /72b3edf317a8d682466c1500...a5e2cad31c8305ed42c41cd61149 117,0MiB [## ] /818e3763e72ef82b28b0552e...b9f163dc601d266e94e46fd26bb0 57,4MiB [# ] /eeffdfafed9f60771b5bf87a...e8bbd16b572f77899c8e689d174d 56,1MiB [# ] /6976ce3ed5fab37382d90467...37578332417ffcf35a1d499eba52 Total disk usage: 3,0GiB Apparent size: 3,0GiB Items: 168678 开头的目录,我必须寻找。进入它并列出内容

0720a07653a57d9...

如您所见,列出了root@T520:/var/lib/docker/aufs/diff# cd 0720a07653a57d938c861cf32f4bee87fa4be61c81c29f12289759f0560aa38f root@T520:/var/lib/docker/aufs/diff/0720a07653a57d938c861cf32f4bee87fa4be61c81c29f12289759f0560aa38f# ls -all -h insgesamt 391M drwxr-xr-x 5 root root 4,0K Feb 23 10:55 . drwxr-xr-x 674 root root 80K Feb 23 10:55 .. -rw-r--r-- 1 root root 391M Feb 23 10:57 a.txt drwx------ 2 root root 4,0K Feb 23 10:55 root -r--r--r-- 1 root root 0 Feb 23 10:55 .wh..wh.aufs drwx------ 2 root root 4,0K Feb 23 10:55 .wh..wh.orph drwx------ 2 root root 4,0K Feb 23 10:55 .wh..wh.plnk 文件。

现在重新运行程序,创建随机文件并重新执行ncdu(只需在ncdu中点击a.txt。)

r应该向您显示,ncdu应该这样做,目录大小没有改变。所以docker fs中的数据只是被覆盖了。如果你选择一个小尺寸它会变小。

那么这对你有什么帮助? 如上所示,文件中的数据更改没有文件增长。你可以找到包含文件系统的女巫目录,并查看容器中添加/更改文件的普通文件结构。

希望这有助于查找容器中的文件。

如果您退出容器并再次使用相同的命令重新启动它,则会创建一个新实例,并创建自己的fs图层。

您可以使用

找到已停止容器的ID
ls

要在删除之前查看找到的内容...

docker ps -a | grep Exited | grep stk/alpine:base | awk '{print $1 }'

附加docker ps -a | grep Exited | grep stk/alpine:base 7b7d3f6e857a stk/alpine:base "sh" 22 minutes ago Exited (0) 2 minutes ago gigantic_swartz 2f51ea988a28 stk/alpine:base "sh" 23 minutes ago Exited (0) 22 minutes ago cranky_euler 4bfafbb034fe stk/alpine:base "sh" 34 minutes ago Exited (0) 25 minutes ago sick_williams 80cd5687fcd7 stk/alpine:base "sh" 44 minutes ago Exited (137) 37 minutes ago determined_panini a2179a8dd543 stk/alpine:base "sh" 58 minutes ago Exited (130) 44 minutes ago agitated_shockley 8596cd310292 stk/alpine:base "sh" 3 days ago Exited (137) 3 days ago dreamy_murdock 33db61a7830b stk/alpine:base "sh" 3 days ago Exited (0) 3 days ago desperate_hodgkin 2f96c15dc8a1 stk/alpine:base "sh" 2 weeks ago Exited (0) 2 weeks ago determined_babbage 以删除它们

一线解决方案

| xargs -r docker rm

Docker将检查其他图片是否未使用这些图片,并在docker ps -a | grep Exited | grep stk/alpine:base | awk '{print $1 }' | xargs -r docker rm 执行docker rmi时无法删除。但在这种情况下,您希望删除容器而不是图像。因此,请使用rm代替rmi(我更新了答案)

享受