You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker无法启动(服务器空间不足):如何手动删除日志与悬空镜像?

手动清理Docker日志与镜像(Docker未运行时)

Got it, let's walk through this step by step—since Docker isn't running, we can't rely on the built-in docker system prune commands, but we can directly target the files that are eating up your server space.

一、清理Docker日志文件

Docker stores container logs by default in the /var/lib/docker/containers/ directory. Each container has its own subfolder, with log files typically named <container-id>-json.log (these are usually the biggest space hogs).

  1. Identify large log files
    Run this command to see which container directories are taking up the most space:

    du -sh /var/lib/docker/containers/*
    

    This will show you the total size of each container's log and data files.

  2. Clear or delete logs

    • If you want to completely remove the log files (safe since Docker isn't running):
      rm -rf /var/lib/docker/containers/<your-container-id>/*.log
      
      Replace <your-container-id> with the actual ID from the previous step.
    • If you prefer to keep the log files but empty their content (avoids potential issues if Docker recreates the container later):
      truncate -s 0 /var/lib/docker/containers/<your-container-id>/*.log
      

二、清理Docker镜像与悬空镜像

Docker images and their layered data are stored in /var/lib/docker/images/ (metadata) and /var/lib/docker/overlay2/ (actual file system layers—most modern setups use the overlay2 driver).

  1. Check image and layer space usage
    First, get a clear sense of where the space is being consumed:

    du -sh /var/lib/docker/images/*
    du -sh /var/lib/docker/overlay2/*
    
  2. Delete dangling images
    Dangling images are untagged images that aren't referenced by any container or other image. To remove them:

    • First, list all image IDs stored on your system:
      ls /var/lib/docker/images/*/imagedb/content/sha256/
      
    • Next, extract image IDs that are actually used by containers. Each container directory in /var/lib/docker/containers/ has a config.v2.json file referencing its image ID:
      grep -r "Image" /var/lib/docker/containers/*/config.v2.json | awk -F '"' '{print $4}' | cut -c7- | sort -u
      
    • Now, delete any image ID from the first list that doesn't appear in the second list (these are your dangling images):
      rm -rf /var/lib/docker/images/*/imagedb/content/sha256/<dangling-image-id>
      rm -rf /var/lib/docker/overlay2/<layer-id-associated-with-image>
      
      Note: Overlay2 layer IDs link to image layers—if you're unsure, you can safely delete overlay2 directories that don't have a corresponding entry in /var/lib/docker/image/overlay2/layerdb/sha256/.
  3. Delete unused volumes (optional)
    If you have volumes you no longer need, you can delete them from:

    rm -rf /var/lib/docker/volumes/<volume-id>
    

    List all volumes with ls /var/lib/docker/volumes/ to identify which ones to remove.

Important Notes

  • Always back up any critical data before deleting files manually.
  • After cleaning, restart Docker with sudo systemctl start docker—it should now have enough space to run.
  • Once Docker is running, set up log rotation (e.g., via Docker's log-driver configuration) to prevent this issue from recurring.

内容的提问来源于stack exchange,提问作者Ayo K

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 12:33:11