在使用docker一段時間後突然出現某個容器進程掛掉,然後再去重新啓動都會失敗,這是可能需要看看是不是docker把磁盤佔滿了。下面詳細介紹一下我遇到docker佔滿磁盤的一次經歷。
某天上午測試突然反饋內網測試環境業務出現問題,功能不能正常使用,於是趕緊查看日誌定位,發現是zookeeper連接異常了,zookeeper是使用docker部署,一般不會出現問題。
- 執行docker ps 命令發現 zookeeper已經掛掉了,然後執行 docker ps -a 查看zookeeper的容器id,接着執行docker start <zookeerper 容器id> ,發現zookeeper已經起不來了
2. 查看zookeeper的日誌
在/var/lib/docker/containers目錄下 感覺容器id找對應的log,根據error異常百度發現是磁盤滿了
3. 查看服務器的磁盤使用情況
執行df命令
[root@apollo containers]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/vg-root 15971232 12841532 2295348 100% /
/dev/sda1 487634 195848 262090 43% /boot
tmpfs 800900 0 800900 0% /run/user/0
/dev/sdb 103080888 61464 97760160 1% /data2
overlay 15971232 12841532 2295348 100%
/var/lib/docker/overlay2/7f45979e4ea63e8a4a8c8228c3177d33b26215462301fdccb4eb456e4d753c54/merged
所有的overlay 的Use% 都提示100% ,根目錄/ 的Use%也提示100%,確實磁盤佔滿了
4.一層一層查看那個文件夾或文件佔用磁盤最多
在根目錄下執行命令
du -h --max-depth=1
一層一層查看,發現/var/lib/docker佔用的磁盤空間最多
5. 清理docker 佔用的磁盤
經查看最大的文件夾有/var/lib/docker/volumes,/var/lib/docker/overlay2,/var/lib/docker/containers
containers中存放了所有的容器的日誌,可以用容器的id找到對應的目錄,如果容器已經停止或刪除,可以直接刪除對應的文件夾。
volumes,overlay2 佔用的磁盤空間最多,很明顯就是要刪除這個兩個目錄下面的東西,但是這裏對應的文件夾無法和容器id對應上來,所以不能隨便刪除。只能先使用docker的命令來刪除。
- 先執行 docker system prune命令 ,發現清理出來一些空間了。
$ 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
- 然後執行docker volume prune,刪除沒有使用的數據卷,但是還有很多的空間佔用
$ docker volume prune
WARNING! This will remove all local volumes not used by at least one container.
- 詳細查看volume文件夾
執行du -h --max-depth=1 查看裏面佔用磁盤最多的一個文件夾
打開進入一看,原來是數據庫,原來根源是數據庫佔用將近一半的磁盤空間,於是清理了一下數據庫,磁盤空間釋放了一半出來了。
- 詳細查看overlay2 文件夾
overlay2也佔用了不少的空間,經過排除 一個容器可能會關聯多個 overlay2 下面的目錄,無從下手,放棄了。
最後記錄幾個有用的命令:
1.docker system df ----查看Docker佔用分佈
2. docker stats ---可以查看docker容器的內存佔用
docker stats ---- Display a live stream of container(s) resource usage statistics
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
5f10308a7d93 zookeeper 0.12% 68.73MiB / 7.79GiB 0.86% 1.5kB / 0B 5.97MB / 32.8kB 38
70062327fc72 apollo-portal 0.17% 402.8MiB / 7.79GiB 5.05% 5.5GB / 3.94GB 86.5MB / 24.6kB 53
808793d179ee apollo-admin 0.16% 442.1MiB / 7.79GiB 5.54% 18.1GB / 16.1GB 118MB / 24.6kB 59
7feade55a442 apollo-configser 1.16% 452.7MiB / 7.79GiB 5.68% 84.5GB / 53.6GB 158MB / 24.6kB 96
89f85b83a29b mysql 1.10% 659.9MiB / 7.79GiB 8.27% 77.4GB / 124GB 16.1GB / 2.31TB 178
CONTAINER ID and Name :the ID and name of the container
CPU % and MEM % :the percentage of the host’s CPU and memory the container is using
MEM USAGE / LIMIT:the total memory the container is using, and the total amount of memory it is allowed to use
NET I/O:The amount of data the container has sent and received over its network interface
BLOCK I/O:The amount of data the container has read to and written from block devices on the host
PIDs:the number of processes or threads the container has created
參考:https://docs.docker.com/engine/reference/commandline/stats/?spm=a2c6h.13066369.0.0.7c314e3c1ttce3