命令: docker system prune
[root@idc02-e01-so-toc /]# 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
# 第二种清理更彻底,会删除所有未在使用中的卷,镜像等
docker system prune -a
补充:
后来没多久磁盘再一次满了,使用 du -sh ./* 找占用空间大的目录,最终找到docker的containers目录,占用150多G.
此目录下进去后是很多docker 的ID为名的目录,再次进入到一个后发现有一个4c1965d7dc8a07923beafee39ae35fa62ad3714fbeae4b53647104dbc651023c-json.log
日志文件,大概十多个G左右,百度后发现可以直接删除,然后就rm -rf了,重启容器后空间释放.
定时清理空间脚本,BackupLog是系统日志存放位置.不需调用此函数
#!/bin/bash
BackupLog(){
backupdir='backuplogs'
atime=`date +%F`
cd /var/www
if [ -d $backupdir ];then
echo "directory already exists"
else
mkdir $backupdir
fi
cp -r logs $backupdir/logs
tar zcf logs-$atime.tar.gz $backupdir/logs && rm -rf ./$backupdir/logs && mv logs-$atime.tar.gz $backupdir
# 清空现有日志
cd /var/www/logs
for i in `ls`;do
echo "" >$i
done
# 删除三十天之前的
cd /var/www/$backupdir
find ./ -type f -name "logs-*.tar.gz" -atime 30|xargs -n 1 rm -rf
}
DockerLogRemove(){
LogName='DockerRemove.log'
cd /data/docker/containers
du -sh ./* |grep 'G' >$LogName
if [ $? -eq 0 ];then
echo "not null"
else
return
fi
for i in `awk '{print $2}' $LogName`;do
DockerId=${i:2:12}
rm -rf $i/${i:2:100}-json.log
docker restart $DockerId
done
}
start(){
echo "start [`date +%F%T`]" >>/root/remove-log.log
BackupLog
DockerLogRemove
echo "stop [`date +%F%T`]" >>/root/remove-log.log
}