Skip to content

fix(down): keep a volume the project does not own on down -v - #14374

Merged
ndeloof merged 3 commits into
docker:mainfrom
htoyoda18:fix/down-volume-other-project
Oct 26, 2026
Merged

ndeloof merged 3 commits into
docker:mainfrom
htoyoda18:fix/down-volume-other-project

Conversation

@htoyoda18

@htoyoda18 htoyoda18 commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

What this PR does

down -v no longer removes a named volume the project does not own: one labeled for another project, or one created outside Compose (no project label). Such a volume is skipped with a warning. This is the rule up already applies (ownedConfigHash): it reuses such a volume untouched. If the volume cannot be inspected, down now fails instead of removing a volume it could not check.

Related issue

Fixes #14373

Changes made

  • removeVolume (pkg/compose/down.go) checks the com.docker.compose.project label on the volume it already inspects, and skips the volume unless it belongs to this project. The Skipped warning tells the two cases apart: Volume belongs to project "x" vs Volume was not created by Docker Compose.
  • An inspect error other than NotFound is returned (failed to inspect volume <name>: <err>) and the volume is kept, the same way removeNetwork fails on an inspect error.
  • Unit tests: TestEnsureVolumesDown_SkipsVolumeNotOwnedByProject and TestEnsureVolumesDown_KeepsVolumeWhenInspectFails. TestDownRemoveVolumes now returns a labeled volume from its inspect mock.
  • E2E: TestDownVolumesKeepsVolumesNotOwned, plus VolumeExists / VolumeAbsent checks in pkg/e2e/checks.go.

Testing done

  • Unit tests pass: make test
  • E2E tests pass: make e2e-compose or make e2e-compose-standalone
  • Linting passes: make lint
  • Code formatted: make fmt
  • Manually tested (describe):

AI tool used (if applicable)

AI Tool:

Additional context

(not mandatory) A picture of a cute animal, if possible in relation to what you did

image

@htoyoda18
htoyoda18 marked this pull request as ready for review October 21, 2026 19:27
@htoyoda18
htoyoda18 requested review from a team as code owners October 21, 2026 19:27
@htoyoda18
htoyoda18 requested review from glours and ndeloof October 21, 2026 19:27

@ndeloof ndeloof left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix works as described: I reproduced #14373 on a real engine (down -v in the second project removes the first project's volume on main, skips it with this PR). Two changes requested inline: unlabeled volumes, and failing closed when the inspect fails.

Suggestion: add an e2e scenario (NewScenario, see pkg/e2e/SCENARIO.md) with two projects sharing a volume name:, checking that down -v in the second one keeps the first one's volume, and also an unlabeled one. It captures the intent and validates the behavior on a real engine rather than on mocks.

For reference, up already applies exactly this rule (ownedConfigHash in observed_state.go): a volume whose project label differs from the current project, or is missing, is reused untouched and never recreated. down -v should follow the same rule.

Comment thread pkg/compose/down.go Outdated
// A volume declared with an explicit name can be owned by another project
// sharing that name: up only reuses it (see warnUnmanagedVolumes), so down
// must not remove it either.
if owner := res.Volume.Labels[api.ProjectLabel]; err == nil && owner != "" && owner != projectName {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The point of this PR is that Compose never removes a volume it did not create, so a volume without the project label should be skipped too, not only one labeled for another project: it carries no ownership either. up already treats it as unmanaged (already exists but was not created by Docker Compose) and reuses it untouched, so down -v removing it is inconsistent. I checked on a real engine: up warns, then down -v removes the volume.

So the rule becomes owner != projectName, with a message that tells the two cases apart, e.g. Volume belongs to project "x" vs Volume was not created by Docker Compose (no project label). The unit test should then expect unlabeled-data to be skipped as well.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 8ebcae8: the rule is now owner != projectName, so a volume without the project label is skipped too. The warning tells the two cases apart: Volume belongs to project "x" vs Volume was not created by Docker Compose. The unit test now expects unlabeled-data to be skipped, and TestDownRemoveVolumes returns a labeled volume from its inspect mock.

Comment thread pkg/compose/down.go
resource := "Volume " + id

_, err := s.apiClient().VolumeInspect(ctx, id, client.VolumeInspectOptions{})
res, err := s.apiClient().VolumeInspect(ctx, id, client.VolumeInspectOptions{})

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the inspect fails with anything but NotFound, err is dropped and the volume is removed without any ownership check. The check should fail closed: return the error (or at least emit an error event, like removeNetwork does) rather than remove a volume we could not verify. A test case for it would be welcome.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 8d6fae0: an inspect error other than NotFound is now returned (failed to inspect volume <name>: <err>) and the volume is left in place. As with removeNetwork, this fails down. TestEnsureVolumesDown_KeepsVolumeWhenInspectFails covers it.

Signed-off-by: hiroto.toyoda <hiroto.toyoda@dena.com>
Signed-off-by: hiroto.toyoda <hiroto.toyoda@dena.com>
Signed-off-by: hiroto.toyoda <hiroto.toyoda@dena.com>
@htoyoda18
htoyoda18 force-pushed the fix/down-volume-other-project branch from a02c604 to bd7684a Compare October 22, 2026 09:28
@htoyoda18

htoyoda18 commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor Author

@ndeloof
Thanks for the review and for reproducing it on a real engine. I've addressed both points and added the e2e scenario. The branch is now 3 commits (force-pushed):

  1. fix(down): keep a volume the project does not own (labeled for another project, or unlabeled)
  2. fix(down): fail closed when the volume cannot be inspected
  3. test(e2e): TestDownVolumesKeepsVolumesNotOwned, with new VolumeExists / VolumeAbsent checks in checks.go

A scenario drives a single project, so the scenario creates the "other project" volume with docker volume create --label com.docker.compose.project=<other>. That is the same label another project's up would set. On main the scenario fails (expected volume "...-shared" exists: ... no such volume); with this PR it passes.

@htoyoda18
htoyoda18 requested a review from ndeloof October 22, 2026 09:32
@htoyoda18 htoyoda18 changed the title fix(down): keep a volume owned by another project on down -v fix(down): keep a volume the project does not own on down -v Oct 22, 2026
@ndeloof
ndeloof enabled auto-merge (rebase) October 26, 2026 10:58
@ndeloof
ndeloof merged commit fab1bb4 into docker:main Oct 26, 2026
61 checks passed
@codecov

codecov Bot commented Oct 26, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] down -v removes a named volume owned by another project

2 participants