diff --git a/cli/command/service/update.go b/cli/command/service/update.go index 13af4bd746cf..5b0df486e98a 100644 --- a/cli/command/service/update.go +++ b/cli/command/service/update.go @@ -352,8 +352,10 @@ func updateService(ctx context.Context, apiClient client.NetworkAPIClient, flags if err := updateIsolation(flagIsolation, &cspec.Isolation); err != nil { return err } - if err := updateMounts(flags, &cspec.Mounts); err != nil { - return err + if anyChanged(flags, flagMountAdd, flagMountRemove) { + if err := updateMounts(flags, &cspec.Mounts); err != nil { + return err + } } updateSysCtls(flags, &task.ContainerSpec.Sysctls) diff --git a/cli/command/service/update_test.go b/cli/command/service/update_test.go index 6158820951b1..38dd29cf9aa2 100644 --- a/cli/command/service/update_test.go +++ b/cli/command/service/update_test.go @@ -250,6 +250,32 @@ func TestUpdateMounts(t *testing.T) { assert.Check(t, is.Equal("/tokeep", mounts[1].Target)) } +func TestUpdateServiceForcePreservesMountOrder(t *testing.T) { + flags := newUpdateCommand(nil).Flags() + assert.NilError(t, flags.Set("force", "true")) + + spec := &swarm.ServiceSpec{ + TaskTemplate: swarm.TaskSpec{ + ContainerSpec: &swarm.ContainerSpec{ + Mounts: []mount.Mount{ + {Type: mount.TypeVolume, Source: "z-volume", Target: "/data/z"}, + {Type: mount.TypeVolume, Source: "a-volume", Target: "/data/a"}, + {Type: mount.TypeVolume, Source: "m-volume", Target: "/data/m"}, + }, + }, + }, + } + + err := updateService(context.Background(), nil, flags, spec) + assert.NilError(t, err) + assert.Equal(t, spec.TaskTemplate.ForceUpdate, uint64(1)) + assert.DeepEqual(t, spec.TaskTemplate.ContainerSpec.Mounts, []mount.Mount{ + {Type: mount.TypeVolume, Source: "z-volume", Target: "/data/z"}, + {Type: mount.TypeVolume, Source: "a-volume", Target: "/data/a"}, + {Type: mount.TypeVolume, Source: "m-volume", Target: "/data/m"}, + }) +} + func TestUpdateMountsWithDuplicateMounts(t *testing.T) { flags := newUpdateCommand(nil).Flags() flags.Set("mount-add", "type=volume,source=vol4,target=/toadd")