How to keep /var/lib/docker/overlay2 clean

I recently installed docker on an ubuntu, but I see that there are certain files that are generated in the following path /var/lib/docker/overlay2/, and recently I filled up the root disk partition, causing the application to collapse, could you help me to avoid this type of event in the future, such as what commands to apply or what actions should be performed. Thank you

So what’s the exact question here?

If you pull image or create containers it will store the date in the storage driver folder inside the data-root folder. In your case the data-root is /var/lib/docker and your storage driver is overlay2, so these data you seem to not have mounted a different filesystem into that path.

So either mount a different filesystem into /var/lib/docker or change the data-root to point to a dfifferent filesystem.

Thank you very much for answering, another alternative to mounting another route, there are docker commands that can help me clean up when the containers are running. Thanks

A running container can not be cleaned up. A removed container is automatically cleaned up.
Image are not removed, unless you remove them with docker image rm ${container name or id} or use docker image prune -a, which will remove all unused image (not referenced by an existing container!)

You can also use docker system prune -a which will remove all unused networks, volumes, and images that are not referenced by an existing container. Be careful if you temporarily removed a swarm stack or compose project that have volumes, those volumes would be affected and removed as well (as no container actually references them) and not exist when you redeploy the swarm stack or the compose project…

I renamed your topic your objective is neither related to Swarm, or Overlay. Furthermore, I moved it to the docker engine category and removed the swarm and compose tags.