Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy

Bits of .NET

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

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
Docker

Docker: Fix a Compose Stack Silently Reusing a Stale Anonymous Volume Because a Service Was Renamed Without Renaming Its Volume

- 02.11.26 - ErcanOPAK

🐳 Fix a Compose Stack Silently Reusing a Stale Anonymous Volume Because a Service Was Renamed Without Renaming Its Volume

A volume declared under Compose’s top-level volumes: section gets its actual Docker-level name from the volume’s own key (or an explicit name: field) — never from whichever service happens to mount it. Rename a service in docker-compose.yml as part of a refactor, while leaving the volume key underneath it untouched, and Compose has no reason to treat it as a fresh volume: the exact same named volume, with all of its existing data, gets mounted into the renamed service exactly as before. For a team expecting a service rename (especially one paired with a major image version bump) to mean a clean slate, this quietly resurfaces the previous service’s entire dataset instead.

🔎 The Problem

# v1 - service named "postgres"
services:
  postgres:
    image: postgres:16
    volumes:
      - pgdata:/var/lib/postgresql/data
volumes:
  pgdata:

# v2 - service renamed to "db" for clarity, volume key left unchanged
services:
  db:
    image: postgres:17   # new major version, expecting a clean slate
    volumes:
      - pgdata:/var/lib/postgresql/data
volumes:
  pgdata:   # <- same key, Docker still resolves this to myproject_pgdata

# docker volume ls
# myproject_pgdata   <- still present, still full of the OLD service's data

✅ Fix: Rename the Volume Too, or Explicitly Drop It, When You Actually Want a Clean Slate

  • Remember that a Compose volume’s real Docker identity comes from its key under the top-level volumes: section (or an explicit name: field), never from whichever service happens to mount it — renaming a service’s key does nothing to the volume sitting underneath it.
  • If the rename is meant to represent a genuinely fresh deployment, rename the volume key (or add an explicit name:) at the same time, or remove the old volume with docker volume rm after confirming you don’t still need its data, rather than assuming a new service key implies a new volume.
  • Run docker compose down -v only when you explicitly want to discard the named volume along with the containers — a plain docker compose down intentionally leaves named volumes in place by design, which is exactly how the “stale” data on a renamed service survives.

🚨 Why This Is Dangerous

  • A service rename done as part of a blue/green cutover or a schema-breaking migration can silently inherit the old service’s entire dataset instead of starting clean, so what looked like an isolated new deployment is actually still reading from and writing to the exact same disk as the thing it was meant to replace.

In Compose, a volume belongs to its name, not to whichever service happens to be mounting it this week — rename the service all you want, the data doesn’t even notice.

— DevOps Engineer

Related posts:

.dockerignore: The Secret to Lightning-Fast Docker Builds

.dockerignore: The Secret to Lightning-Fast Docker Builds

Docker Build Is Slow Even With Cache

Docker Build Is Slow Even With Cache

Docker: Use Docker Init to Generate Dockerfiles Automatically

Docker: Use Docker Init to Generate Dockerfiles Automatically

Views: 8

Post navigation

Git: Fix git diff –color-moved Silently Failing to Detect a Moved Block Because Reindentation Changed Every Line’s Leading Whitespace
ASP.NET Core: Fix Output Caching’s Vary-By-Query Rule Silently Caching Two Different Responses Under One Key Because Query String Keys Are Compared Case-Sensitively

Leave a Reply Cancel reply

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

November 2026
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« Oct    

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,055)
  • Get the User Name and Domain Name from an Email Address in SQL (980)
  • How to add default value for Entity Framework migrations for DateTime and Bool (976)
  • Get the First and Last Word from a String or Sentence in SQL (857)
  • How to select distinct rows in a datatable in C# (852)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (653)
  • Add Constraint to SQL Table to ensure email contains @ (607)
  • Average of all values in a column that are not zero in SQL (569)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (546)
  • Find numbers with more than two decimal places in SQL (475)

Recent Posts

  • C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon
  • Docker: Fix a Stale COPY Layer After Editing .dockerignore
  • Photoshop: Fix a Pattern Overlay Scaling Wrong on a High-DPI File
  • SQL Server: Fix a Computed Column Index Skipped by Implicit Cast
  • HTML5: Fix a required Input Inside a Hidden Tab Blocking Submit
  • SQL Server: Fix ROW_NUMBER Ties Reordering Between Identical Runs
  • C#: Fix an Enum HasFlag Returning True for the Zero Value
  • WordPress: Fix Featured Images Vanishing After a CDN Migration
  • Photoshop: Fix Content-Aware Fill Smearing a Repeating Pattern
  • Asp.Net Core: Fix Antiforgery Tokens Rejected Behind a Reverse Proxy

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,055)
  • Get the User Name and Domain Name from an Email Address in SQL (980)
  • How to add default value for Entity Framework migrations for DateTime and Bool (976)
  • Get the First and Last Word from a String or Sentence in SQL (857)
  • How to select distinct rows in a datatable in C# (852)

Recent Posts

  • C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon
  • Docker: Fix a Stale COPY Layer After Editing .dockerignore
  • Photoshop: Fix a Pattern Overlay Scaling Wrong on a High-DPI File
  • SQL Server: Fix a Computed Column Index Skipped by Implicit Cast
  • HTML5: Fix a required Input Inside a Hidden Tab Blocking Submit

Social

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