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).
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.
Clear or delete logs
- If you want to completely remove the log files (safe since Docker isn't running):
Replacerm -rf /var/lib/docker/containers/<your-container-id>/*.log<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
- If you want to completely remove the log files (safe since Docker isn't running):
二、清理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).
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/*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 aconfig.v2.jsonfile 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):
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 inrm -rf /var/lib/docker/images/*/imagedb/content/sha256/<dangling-image-id> rm -rf /var/lib/docker/overlay2/<layer-id-associated-with-image>/var/lib/docker/image/overlay2/layerdb/sha256/.
- First, list all image IDs stored on your system:
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-driverconfiguration) to prevent this issue from recurring.
内容的提问来源于stack exchange,提问作者Ayo K

