diff --git a/daemon/rename.go b/daemon/rename.go index 1e3cd5370c..6d8293f127 100644 --- a/daemon/rename.go +++ b/daemon/rename.go @@ -1,8 +1,6 @@ package daemon -import ( - "github.com/docker/docker/engine" -) +import "github.com/docker/docker/engine" func (daemon *Daemon) ContainerRename(job *engine.Job) engine.Status { if len(job.Args) != 2 { @@ -26,9 +24,21 @@ func (daemon *Daemon) ContainerRename(job *engine.Job) engine.Status { container.Name = newName + undo := func() { + container.Name = oldName + daemon.reserveName(container.ID, oldName) + daemon.containerGraph.Delete(newName) + } + if err := daemon.containerGraph.Delete(oldName); err != nil { + undo() return job.Errorf("Failed to delete container %q: %v", oldName, err) } + if err := container.toDisk(); err != nil { + undo() + return job.Error(err) + } + return engine.StatusOK } diff --git a/integration-cli/docker_cli_daemon_test.go b/integration-cli/docker_cli_daemon_test.go index 31f55296a4..8ba1b85b1b 100644 --- a/integration-cli/docker_cli_daemon_test.go +++ b/integration-cli/docker_cli_daemon_test.go @@ -534,3 +534,29 @@ func TestDaemonUlimitDefaults(t *testing.T) { logDone("daemon - default ulimits are applied") } + +// #11315 +func TestDaemonRestartRenameContainer(t *testing.T) { + d := NewDaemon(t) + if err := d.StartWithBusybox(); err != nil { + t.Fatal(err) + } + + if out, err := d.Cmd("run", "--name=test", "busybox"); err != nil { + t.Fatal(err, out) + } + + if out, err := d.Cmd("rename", "test", "test2"); err != nil { + t.Fatal(err, out) + } + + if err := d.Restart(); err != nil { + t.Fatal(err) + } + + if out, err := d.Cmd("start", "test2"); err != nil { + t.Fatal(err, out) + } + + logDone("daemon - rename persists through daemon restart") +}