🐳 Docker: Fix a Volume Mount Reverting Permissions After Restart
A named volume’s ownership is set once, the first time Docker actually creates the underlying directory on the host, which happens on the very first container start that mounts it, not on every subsequent start. If an image’s ENTRYPOINT runs chown or chmod against that mount path expecting to fix permissions every time the container boots, and the volume already existed from a previous run under a different ownership, Docker doesn’t recreate or reset its contents, so a restart after the image’s USER directive changes, or after copying the volume to a new host, leaves the mount with whatever ownership it had the first time, regardless of what the current image now expects.
🔎 The Problem
# First run (as root): creates volume with root:root ownership docker run -d -v applog:/var/log/app myapp:v1 # Later: image updated to run as a non-root user docker run -d -v applog:/var/log/app myapp:v2 # myapp:v2's USER appuser can't write to /var/log/app - # the volume still has root:root ownership from its very # first creation and Docker never re-applies permissions # on subsequent starts
✅ Fix: Re-Apply Ownership Explicitly, Every Start
- Run an explicit chown as part of the container’s entrypoint (or a one-off init container in Compose) every time it starts, rather than assuming Docker reapplies the image’s intended ownership to an already-existing volume.
- When switching an image to run as a non-root USER, run a one-time
docker run --rm -v applog:/data busybox chown -R appuser /dataagainst the existing volume before deploying the new image version. - For a fresh deployment, let the image’s own Dockerfile create the mount path with correct ownership before VOLUME is declared, since a path that exists in the image layer with the right owner is what Docker copies into a brand-new named volume on its very first use.
🚨 Why This Is Dangerous
- A silent permission mismatch that only appears after a restart, not on the very first deploy, can pass every initial smoke test cleanly and then take production logging or data writes down the next time the container happens to restart.
A named volume remembers the ownership it was born with — your image’s current USER directive doesn’t get a vote on that after the fact.




