一、查找docker工作根目录
[root@prd-apm001 ~]$ docker info |grep "Docker Root Dir"
Docker Root Dir: /var/lib/docker
二、查看工作目录占用空间大小
[root@prd-apm001 ~]$ du -sh /var/lib/docker
47G /var/lib/docker
三. 清理docker占用空间
#1.查看docker占用的磁盘空间
[root@prd-apm001 ~]$ docker system df
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 9 8 3.356GB 638.9MB (19%)
Containers 11 8 294.6MB 272.1MB (92%)
Local Volumes 13 13 962.6MB 0B (0%)
Build Cache 0 0 0B 0B
#2.清理docker占用空间
命令docker system prune可以用于清理docker占用的空间,删除关闭的容器、无用的数据卷和网络,以及dangling镜像(即无tag的镜像)
[root@prd-apm001 ~]$ docker system prune
WARNING! This will remove:
- all stopped containers
- all networks not used by at least one container
- all dangling images
- all dangling build cache
Are you sure you want to continue? [y/N] y
Deleted Containers:
c01e9e12feea45d0406773d66aa779140ff8dc61fa56b58856fa9af1aaba103a
f93fb529524cbed5f24d8b8843a4699c072011b3e10e1bf050befd86bae70d5a
5ece3517980654443ff95bd3421c41666a8b552d5c4589d73cc224a0461ef8f3
Total reclaimed space: 272.1MB
#3.命令docker system prune -a可以清理所有没有被使用的docker镜像,即使容器只是暂时停止的状态。使用该命令之前请再三确认,避免删除重要的镜像等文件。
四、迁移docker根目录
#1.停止docker服务
[root@prd-apm001 ~]$ systemctl stop docker
Warning: Stopping docker.service, but it can still be activated by:
docker.socket
#2.查看磁盘占用情况
查看本机磁盘的占用情况,找到合适的、空间足够大的磁盘来作为docker的根目录。
[root@prd-apm001 /data]$ df -h
文件系统 容量 已用 可用 已用% 挂载点
devtmpfs 7.5G 0 7.5G 0% /dev
tmpfs 7.5G 0 7.5G 0% /dev/shm
tmpfs 7.5G 409M 7.1G 6% /run
tmpfs 7.5G 0 7.5G 0% /sys/fs/cgroup
/dev/nvme0n1p1 50G 7.4G 43G 15% /
/dev/nvme1n1p1 200G 55G 146G 28% /data
tmpfs 1.5G 0 1.5G 0% /run/user/1000
overlay 50G 7.4G 43G 15% /var/lib/docker/overlay2/3a032c94048c5c61f7d5007ac7877ae4e1f55e4ef34d7f2bcc9d47ad4c4e5d88/merged
overlay 50G 7.4G 43G 15% /var/lib/docker/overlay2/99c97e176d3408cc2d8eb962e655c4f148c48d6ea6825f074da229a1cf27a982/merged
overlay 50G 7.4G 43G 15% /var/lib/docker/overlay2/a1adeecb8e64017fa96e9e60aac0b5e7b9b5992a04e3f7bb3eab33d2ec26e6b4/merged
overlay 50G 7.4G 43G 15% /var/lib/docker/overlay2/9407232ce5b24eec0702ec4fb766721a2f34e6fb8059b313a958b9df46746a77/merged
overlay 50G 7.4G 43G 15% /var/lib/docker/overlay2/a64463c613581381fbd06575c8e41849a88c0b0867fbb4b8e6eb62a69eb2f25c/merged
overlay 50G 7.4G 43G 15% /var/lib/docker/overlay2/1b0e9790b234cf03265b706dbe1d0f37da8c28c100bcbacf6f74dac94ff4aecf/merged
overlay 50G 7.4G 43G 15% /var/lib/docker/overlay2/7728159c2bfd9a4f9fadc00de6479ffec423535633dcbbbc6435a3560bce9532/merged
overlay 50G 7.4G 43G 15% /var/lib/docker/overlay2/c3e67dd83dbed48e285b59739d38c5aff38aa86281b9381a22d5bce424cb89ca/merged
overlay 50G 7.4G 43G 15% /var/lib/docker/overlay2/206310494a2247a1b9dd2499089953478f3451ddc6975508c4ad8cf90424dae7/merged
overlay 50G 7.4G 43G 15% /var/lib/docker/overlay2/e6f6d9b4fd38dda9f44c91f804668e096c0b2aded2e2bd07f771f761ca7708f0/merged
overlay 50G 7.4G 43G 15% /var/lib/docker/overlay2/f41c298e96fde31a8b87f767bd868715ef940d73aa8a0ad1346b2eea732a3487/merged
#3.拷贝旧目录文件到新目录下
[root@prd-apm001 ~]$ mkdir /data/pinpoint-apm
[root@prd-apm001 ~]$ mv /var/lib/docker/ /data/pinpoint-apm
五、重新设置 Docker Root Dir
#1.重新设置 Docker Root Dir
[root@prd-apm001 ~]$ cd /data/pinpoint-apm/docker/
[root@prd-apm001 /data/pinpoint-apm/docker]$ vim /usr/lib/systemd/system/docker.service
# 在 ExecStart=/usr/bin/dockerd 后追加 --graph=/data/pinpoint-apm/docker,注意如果本来后面有其他参数不要搞乱了
# 示例如下(后面其他参数是我个人配置别的使用的请忽略)
ExecStart=/usr/bin/dockerd --graph=/data/pinpoint-apm/docker -H fd:// --containerd=/run/containerd/containerd.sock
六、重新加载配置启动服务
[root@prd-apm001 /data/pinpoint-apm/docker]$ systemctl daemon-reload
[root@prd-apm001 /data/pinpoint-apm/docker]$ systemctl start docker.service
七、验证docker根目录是否生效
[root@prd-apm001 /data/pinpoint-apm/docker]$ docker info |grep "Docker Root Dir"
Docker Root Dir: /data/pinpoint-apm/docker