SoFunction
Updated on 2025-04-14

docker overlay2 folder is relatively large processing steps

overlay2It is Docker's default storage driver for managing container and mirror storage. whenoverlay2When folders become very large, they are usually due to the following reasons:

  • Uncleaned images and containers: Unused mirrors, stopped containers, hanging volumes, etc. occupy a lot of space.
  • The log file is too large: The log files generated by the container may take up a lot of space.
  • Data volume not cleaned: The mounted data volume may store a large amount of data.

The following is a cleaningoverlay2Steps to folder:

1. Clean up unused Docker objects

Docker provides commands to clean unused images, containers, networks, and volumes.

Clean up hanging images

docker image prune -f

Clean up unused images, containers, networks, and volumes

docker system prune -f

If you want to clean up all unused objects (including unused volumes), you can use:

docker system prune -a -f --volumes

2. Delete unused containers

List all containers (including those that are stopped):

docker ps -a

Delete unwanted containers:

docker rm <container_id>

3. Delete unused images

List all images:

docker images

Delete unwanted images:

docker rmi <image_id>

4. Clean the log files

If the container's log file is too large, you can limit the log size or clean the log.

Limit log size

When running the container, you can use--log-opt max-sizeand--log-opt max-fileLimit log size:

docker run -d --log-opt max-size=10m --log-opt max-file=3 <image_name>

Clean the log files

Find the container's log files and clean it manually:

docker inspect <container_id> | grep LogPath

Then delete or clear the log file:

truncate -s 0 <log_file_path>

5. Clean up the data volume

List all data volumes:

docker volume ls

Delete unused data volumes:

docker volume prune -f

6. Check and clean the overlay2 directory

If the above method still fails to free up enough space, you can check manuallyoverlay2Directory and clean unused files.

Find a directory that takes up a lot of space

existoverlay2In the directory, use the following command to find a directory with a large space:

cd /var/lib/docker/overlay2du -sh * | sort -rh | head -n 10

Clean up unused files

After making sure that these directories are not used by any containers or images, you can delete them manually.

7. Restart Docker service

After the cleanup is complete, restart the Docker service to ensure that all changes take effect:

systemctl restart docker

8. Preventive measures

To avoidoverlay2The folder becomes too large again, the following measures can be taken:

  • Regular cleaning:usedocker system pruneClean unused objects regularly.
  • Limit log size: Limit the size of the log file when running the container.
  • Using external storage: Store the data generated by the container to external storage (such as NFS, cloud storage, etc.), rather than directly store it in the container.

Summarize

Through the above steps, you can clean up effectivelyoverlay2folder and free disk space. Regular maintenance of Docker environments is the key to avoid insufficient storage space. If the problem persists, consider migrating the Docker data directory to a larger disk partition.

This is the article about how to deal with the larger docker overlay2 folder. For more related docker overlay2 folder content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!