[Docker]清理Docker占用的磁盘空间,迁移 /var/lib/docker 目录所解决的问题

清理空间:

1. Docker System命令

在《谁用光了磁盘?Docker System命令详解》中,我们详细介绍了Docker System命令,它可以用于管理磁盘空间。

docker system df命令,类似于Linux上的df命令,用于查看Docker的磁盘使用情况:

docker system df
TYPE                TOTAL               ACTIVE              SIZE                RECLAIMABLE
Images              147                 36                  7.204GB             3.887GB (53%)
Containers          37                  10                  104.8MB             102.6MB (97%)
Local Volumes       3                   3                   1.421GB             0B (0%)
Build Cache                                                 0B                  0B


可知,Docker镜像占用了7.2GB磁盘,Docker容器占用了104.8MB磁盘,Docker数据卷占用了1.4GB磁盘。

docker system prune命令可以用于清理磁盘,删除关闭的容器、无用的数据卷和网络,以及dangling镜像(即无tag的镜像)。docker system prune -a命令清理得更加彻底,可以将没有容器使用Docker镜像都删掉。注意,这两个命令会把你暂时关闭的容器,以及暂时没有用到的Docker镜像都删掉了……所以使用之前一定要想清楚吶。

执行docker system prune -a命令之后,Docker占用的磁盘空间减少了很多:

docker system df
TYPE                TOTAL               ACTIVE              SIZE                RECLAIMABLE
Images              10                  10                  2.271GB             630.7MB (27%)
Containers          10                  10                  2.211MB             0B (0%)
Local Volumes       3                   3                   1.421GB             0B (0%)
Build Cache                                                 0B                  0B

2. 手动清理Docker镜像/容器/数据卷

对于旧版的Docker(版本1.13之前),是没有Docker System命令的,因此需要进行手动清理。这里给出几个常用的命令:

删除所有关闭的容器

docker ps -a | grep Exit | cut -d ' ' -f 1 | xargs docker rm


删除所有dangling镜像(即无tag的镜像)

docker rmi $(docker images | grep "^<none>" | awk "{print $3}")


删除所有dangling数据卷(即无用的Volume)

docker volume rm $(docker volume ls -qf dangling=true)

3. 限制容器的日志大小

有一次,当我使用1与2提到的方法清理磁盘之后,发现并没有什么作用,于是,我进行了一系列分析。

在Ubuntu上,Docker的所有相关文件,包括镜像、容器等都保存在/var/lib/docker/目录中:

du -hs /var/lib/docker/
97G /var/lib/docker/


Docker竟然使用了将近100GB磁盘,这也是够了。使用du命令继续查看,可以定位到真正占用这么多磁盘的目录:

92G  /var/lib/docker/containers/a376aa694b22ee497f6fc9f7d15d943de91c853284f8f105ff5ad6c7ddae7a53


docker ps可知,Nginx容器的ID恰好为a376aa694b22,与上面的目录/var/lib/docker/containers/a376aa694b22的前缀一致:

docker ps
CONTAINER ID        IMAGE                                       COMMAND                  CREATED             STATUS              PORTS               NAMES
a376aa694b22        192.168.59.224:5000/nginx:1.12.1            "nginx -g 'daemon off"   9 weeks ago         Up 10 minutes                           nginx


因此,Nginx容器竟然占用了92GB的磁盘。进一步分析可知,真正占用磁盘空间的是Nginx的日志文件。那么这就不难理解了。我们Fundebug每天的数据请求为百万级别,那么日志数据自然非常大。

使用truncate命令,可以将Nginx容器的日志文件“清零”:

truncate -s 0 /var/lib/docker/containers/a376aa694b22ee497f6fc9f7d15d943de91c853284f8f105ff5ad6c7ddae7a53/*-json.log


当然,这个命令只是临时有作用,日志文件迟早又会涨回来。要从根本上解决问题,需要限制Nginx容器的日志文件大小。这个可以通过配置日志的max-size来实现,下面是Nginx容器的docker-compose配置文件:

nginx:
image: nginx:1.12.1
restart: always
logging:
driver: "json-file"
options:
  max-size: "5g"


重启Nginx容器之后,其日志文件的大小就被限制在5GB,再也不用担心了~

4. 重启Docker

有一次,当我清理了镜像、容器以及数据卷之后,发现磁盘空间并没有减少。根据Docker disk usage提到过的建议,我重启了Docker,发现磁盘使用率从83%降到了19%。根据高手指点,这应该是与内核3.13相关的Bug,导致Docker无法清理一些无用目录:
 

it's quite likely that for some reason when those container shutdown, docker couldn't remove the directory because the shm device was busy. This tends to happen often on 3.13 kernel. You may want to update it to the 4.4 version supported on trusty 14.04.5 LTS.


The reason it disappeared after a restart, is that daemon probably tried and succeeded to clean up left over data from stopped containers.

我查看了一下内核版本,发现真的是3.13:

uname -r
3.13.0-86-generic


如果你的内核版本也是3.13,而且清理磁盘没能成功,不妨重启一下Docker。当然,这个晚上操作比较靠谱。

迁移:

一、迁移/var/lib/docker目录

1、du -hs /var/lib/docker/ 命令查看磁盘使用情况。

root@nn0:~$ du -hs /var/lib/docker/
237G    /var/lib/docker/

2、docker system df命令,类似于Linux上的df命令,用于查看Docker的磁盘使用情况:

root@nn0:~$ docker system df
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 7 2 122.2GB 79.07GB (64%)
Containers 2 2 61.96GB 0B (0%)
Local Volumes 0 0 0B 0B
Build Cache 0 0 0B 0B

3、 docker system prune命令可以用于清理磁盘,删除关闭的容器、无用的数据卷和网络,以及dangling镜像(即无tag的镜像)。

复制代码

root@nn0:~$ docker system prune
WARNING! This will remove:
- all stopped containers
- all networks not used by at least one container
- all dangling images
- all build cache
Are you sure you want to continue? [y/N] y
Total reclaimed space: 0B

复制代码

4、 docker system prune -a命令清理得更加彻底,可以将没有容器使用Docker镜像都删掉。注意,这两个命令会把你暂时关闭的容器,以及暂时没有用到的Docker镜像都删掉了…所以使用之前一定要想清楚.。我没用过,因为会清理 没有开启的  Docker 镜像。

5、 迁移 /var/lib/docker 目录。

5.1、 停止docker服务。

systemctl stop docker

5.2、 创建新的docker目录,执行命令df -h,找一个大的磁盘。 我在 /home目录下面建了 /home/docker/lib目录,执行的命令是:

mkdir -p /home/docker/lib

5.3 、迁移/var/lib/docker目录下面的文件到 /home/docker/lib:

rsync -avz /var/lib/docker /home/docker/lib/

5.4、 配置 /etc/systemd/system/docker.service.d/devicemapper.conf。查看 devicemapper.conf 是否存在。如果不存在,就新建。

mkdir -p /etc/systemd/system/docker.service.d/
vi /etc/systemd/system/docker.service.d/devicemapper.conf

5.5 然后在 devicemapper.conf 写入:(同步的时候把父文件夹一并同步过来,实际上的目录应在 /home/docker/lib/docker )

[Service]
ExecStart=
ExecStart=/usr/bin/dockerd --graph=/home/docker/lib/docker

5.6、 重新加载 docker

systemctl daemon-reload

systemctl restart docker

systemctl enable docker

5.7、 为了确认一切顺利,运行

# docker info

命令检查Docker 的根目录.它将被更改为 /home/docker/lib/docker

...
Docker Root Dir: /home/docker/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
...

5.8、 启动成功后,再确认之前的镜像还在:

root@nn0:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
AAA/AAA v2 7331b8651bcc 27 hours ago 3.85GB
BBB/BBB v1 da4a80dd8424 28 hours ago 3.47GB

4.9 确定容器没问题后删除/var/lib/docker/目录中的文件。

二、迁移/var/lib/docker目录后,Docker无法启动 (error initializing graphdriver: driver not supported)

启动信息
[root@nn0 ~]# systemctl start docker
Job for docker.service failed because the control process exited with error code. See "systemctl status docker.service" and "journalctl -xe" for details.

启动详情

复制代码

8月 31 01:47:25 localhost.localdomain systemd[1]: Starting Docker Application Container Engine...
-- Subject: Unit docker.service has begun start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- 
-- Unit docker.service has begun starting up.
8月 31 01:47:25 localhost.localdomain dockerd[2390]: time="2018-08-31T01:47:25.197015872+08:00" level=info msg="libcontainerd: new containerd process, pid: 2393"
8月 31 01:47:26 localhost.localdomain dockerd[2390]: time="2018-08-31T01:47:26.204103195+08:00" level=error msg="[graphdriver] prior storage driver overlay2 failed: driver not supported"
8月 31 01:47:26 localhost.localdomain dockerd[2390]: Error starting daemon: error initializing graphdriver: driver not supported
8月 31 01:47:26 localhost.localdomain systemd[1]: docker.service: main process exited, code=exited, status=1/FAILURE
8月 31 01:47:26 localhost.localdomain systemd[1]: Failed to start Docker Application Container Engine.

复制代码

错误原因:error initializing graphdriver: driver not supported
解决办法:在 /etc/docker 目录下创建daemon.json文件,并且加入以下配置

touch daemon.json
vi daemon.json
{
  "storage-driver": "overlay2",
  "storage-opts": [
    "overlay2.override_kernel_check=true"
  ]
}

再次启动

systemctl start docker

问题解决

三、docker-runc not installed on system 问题

问题描述:

docker运行镜像的时候,报错如下:

[root@nn0 k8s]# docker run -it registry.helloworld.com/test/atsd:latest bash
WARNING: IPv4 forwarding is disabled. Networking will not work.
/usr/bin/docker-current: Error response from daemon: shim error: docker-runc not installed on system.

问题解决:

  经过一番排查,如下解决方案有用:

[root@nn0 k8s]# cd /usr/libexec/docker/
[root@nn0 docker]# ln -s docker-runc-current docker-runc

 

以上,问题解决。 

发布了19 篇原创文章 · 获赞 149 · 访问量 80万+

猜你喜欢

转载自blog.csdn.net/truelove12358/article/details/102949386
今日推荐