fix(down): do not remove a volume whose ownership cannot be checked

Signed-off-by: hiroto.toyoda <hiroto.toyoda@dena.com>
This commit is contained in:
hiroto.toyoda 2026-10-22 18:10:58 +09:00 • committed by Nicolas De loof
parent 4633ab6b35
commit 9060bbe031
2 changed files with 28 additions and 1 deletions

View file

@ -388,10 +388,14 @@ func (s *composeService) removeVolume(ctx context.Context, id string, projectNam
// Already gone
return nil
}
if err != nil {
// without the inspect result its owner can't be checked: keep it
return fmt.Errorf("failed to inspect volume %s: %w", id, err)
}
// A volume declared with an explicit name can belong to another project
// sharing that name, or have been created outside Compose: up only reuses
// such a volume (see ownedConfigHash), so down must not remove it either.
if owner := res.Volume.Labels[api.ProjectLabel]; err == nil && owner != projectName {
if owner := res.Volume.Labels[api.ProjectLabel]; owner != projectName {
reason := fmt.Sprintf("Volume belongs to project %q", owner)
if owner == "" {
reason = "Volume was not created by Docker Compose"

View file

@ -537,6 +537,29 @@ func TestEnsureVolumesDown_SkipsVolumeNotOwnedByProject(t *testing.T) {
})
}
// TestEnsureVolumesDown_KeepsVolumeWhenInspectFails guards #14373: a volume
// whose owner can't be checked must not be removed.
func TestEnsureVolumesDown_KeepsVolumeWhenInspectFails(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
apiClient, cli := prepareMocks(mockCtrl)
svcIface, err := NewComposeService(cli)
assert.NilError(t, err)
svc := svcIface.(*composeService)
project := &types.Project{Name: "prj", Volumes: types.Volumes{
"data": {Name: "prj-data"},
}}
apiClient.EXPECT().VolumeInspect(gomock.Any(), "prj-data", gomock.Any()).
Return(client.VolumeInspectResult{}, errors.New("engine unavailable"))
// no VolumeRemove expectation — a call for prj-data fails the test
ops := svc.ensureVolumesDown(t.Context(), project, nil)
assert.Equal(t, len(ops), 1)
assert.Error(t, ops[0](), "failed to inspect volume prj-data: engine unavailable")
}
func TestDownRemoveImages(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()