Mounted volumes sometimes become empty inside the container

This is, and has been, a recurring issue for me using Docker since 2015 on all three platforms. It is prevalent, or at least, has occurred occasionally, on both Docker for Windows and Docker for Mac. I have not been able to reproduce the issue on Docker for Linux (Ubuntu).

Just like the title states, occasionally I will be working on my website on the Mac host with local files and happily hitting refresh on the browser to instantly see changes in the edited files. The application writes logs which I am able to view instantly on the host. All is well, everything works as expected.

On arbitrarily large projects (~1000s of files and more, Laravel/Symfony PHP or Python/Django websites mainly), sometimes, and for no apparent reason, and without warning, and without any errors, the application dies with errors ranging from 404 to inability to write to the log, whatever.

Docker exec into the container and find the mount volume files are simply gone. The only workaround I have found is to stop the docker process completely and restart it, or occasionally, reboot the machine completely.

I would love to know if others have run into this, what workarounds there are, and how to avoid it. I am not able to switch host OS (this time).

Using the latest Docker for Mac on M1 Mac with 32GB of RAM and 500GB disk, with 16GB / 256GB allocated for Docker to use.

90% of the posts on here and Stack Overflow are either very old, or the user simply forgot to enable sharing.

I have attempted various solutions ranging from using COPY ~/Projects/website:/var/www/html and some combination of fswatch, rsync, ssh-copy, scp, sftp and those workarounds are somewhat useful in theory but require a significant overhead to implement, while also introducing some additional software in the docker environment that is not present on staging/production.

The fact that it mostly works sometimes - but quits for no apparent reason, is maddening.

I have to believe others have experienced this and wonder what can be done about it. I have 100% experienced this issue on Docker for Mac going back at least 3-4 years on both intel and apple silicon. The larger the shared folder, the more likely it happens often.

If I can provide some kind of troubleshooting details the next time it happens, please educate me how, because I am working on a large project now and it’s becoming a real nuisance.

Help me Obi Wan, you’re my only hope.