Search code examples
macosfiledockervirtualboxcorrupt

File mounted as docker volume becomes corrupt when saved outside docker


In my host, I have this dir:

/Users/gezimhome/projects/ziprecipes.net/zip-recipes/src

which I mount to my docker container as:

/usr/share/nginx/html/wordpress/wp-content/plugins/zip-recipes

If I modify the following file (e.g. add a new line) outside of docker: /Users/gezimhome/projects/ziprecipes.net/zip-recipes/src/plugins/VisitorRating/scripts/main.js, I get a syntax error in the browser:

Safari: enter image description here

Chrome: enter image description here

If I go into the docker container and so much as open the file (/usr/share/nginx/html/wordpress/wp-content/plugins/zip-recipes/plugins/VisitorRating/scripts/main.js) and save it again without changes, the error is gone.

I know this is going to be a mind-bender.

I've tried multiple Editors/IDEs in my host to save the file with. It makes no difference.

Update: Wow, I saved the files inside docker both when I edit it from inside docker and when I edit it from outside and did a diff to find no difference:

cp /usr/share/nginx/html/wordpress/wp-content/plugins/zip-recipes/plugins/VisitorRating/scripts/main.js /host.js
# editted the file in same way inside docker
cp /usr/share/nginx/html/wordpress/wp-content/plugins/zip-recipes/plugins/VisitorRating/scripts/main.js /docker.js
root@219a4126d14a:/# diff /host.js /docker.js
root@219a4126d14a:/#

Solution

  • It turns out this was caused by sendfile setting in nginx.

    To fix it, I changed my nginx website config file (/etc/nginx/sites-enabled/nginx_wordpress in my case) to set sendfile off as follows:

    server {
            listen   8080;
            server_name zrdn;
    
            sendfile off;
            ...
    }