Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio
Docker

Docker: Fix a Volume Mount Reverting Permissions After Restart

- 24.11.26 - ErcanOPAK

🐳 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 /data against 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.

— DevOps Engineer

Related posts:

Docker Healthcheck: Monitor Container Health Like a Pro

Docker Healthcheck: Monitor Container Health Like a Pro

Docker: Use Docker Compose Networking for Multi-Container Apps

Docker: Use Docker Compose Networking for Multi-Container Apps

Docker: Configure Logging Drivers for Centralized Log Management

Docker: Configure Logging Drivers for Centralized Log Management

Views: 12
Found this useful? Share it
XLinkedInWhatsApp

Post navigation

Windows 11: Fix Widgets Freezing on a Multi-Monitor Setup
Git: Fix a Shallow Clone Failing to Fetch an Old Tag

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Most Read

  1. 01How to make theater mode the default for Youtube 1,079 views
  2. 02How to add default value for Entity Framework migrations for DateTime and Bool 997 views
  3. 03Get the User Name and Domain Name from an Email Address in SQL 996 views
  4. 04Get the First and Last Word from a String or Sentence in SQL 873 views
  5. 05How to select distinct rows in a datatable in C# 868 views
  6. 06How to enable, disable and check if Service Broker is enabled on a database in SQL Server 671 views
  7. 07Add Constraint to SQL Table to ensure email contains @ 629 views
  8. 08Average of all values in a column that are not zero in SQL 584 views
  9. 09How to use Map Mode for Vertical Scroll Mode in Visual Studio 561 views
Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
Back to top
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com