Merge pull request #24020 from yongtang/23973-rename-with-linked-container
Fix docker rename with linked containers
This commit is contained in:
commit
10fcd30a90
2 changed files with 36 additions and 0 deletions
|
@ -5,6 +5,7 @@ import (
|
|||
"strings"
|
||||
|
||||
"github.com/Sirupsen/logrus"
|
||||
dockercontainer "github.com/docker/docker/container"
|
||||
"github.com/docker/libnetwork"
|
||||
)
|
||||
|
||||
|
@ -40,10 +41,23 @@ func (daemon *Daemon) ContainerRename(oldName, newName string) error {
|
|||
container.Lock()
|
||||
defer container.Unlock()
|
||||
|
||||
links := map[string]*dockercontainer.Container{}
|
||||
for k, v := range daemon.linkIndex.children(container) {
|
||||
if !strings.HasPrefix(k, oldName) {
|
||||
return fmt.Errorf("Linked container %s does not match parent %s", k, oldName)
|
||||
}
|
||||
links[strings.TrimPrefix(k, oldName)] = v
|
||||
}
|
||||
|
||||
if newName, err = daemon.reserveName(container.ID, newName); err != nil {
|
||||
return fmt.Errorf("Error when allocating new name: %v", err)
|
||||
}
|
||||
|
||||
for k, v := range links {
|
||||
daemon.nameIndex.Reserve(newName+k, v.ID)
|
||||
daemon.linkIndex.link(container, v, newName+k)
|
||||
}
|
||||
|
||||
container.Name = newName
|
||||
container.NetworkSettings.IsAnonymousEndpoint = false
|
||||
|
||||
|
@ -52,10 +66,20 @@ func (daemon *Daemon) ContainerRename(oldName, newName string) error {
|
|||
container.Name = oldName
|
||||
container.NetworkSettings.IsAnonymousEndpoint = oldIsAnonymousEndpoint
|
||||
daemon.reserveName(container.ID, oldName)
|
||||
for k, v := range links {
|
||||
daemon.nameIndex.Reserve(oldName+k, v.ID)
|
||||
daemon.linkIndex.link(container, v, oldName+k)
|
||||
daemon.linkIndex.unlink(newName+k, v, container)
|
||||
daemon.nameIndex.Release(newName + k)
|
||||
}
|
||||
daemon.releaseName(newName)
|
||||
}
|
||||
}()
|
||||
|
||||
for k, v := range links {
|
||||
daemon.linkIndex.unlink(oldName+k, v, container)
|
||||
daemon.nameIndex.Release(oldName + k)
|
||||
}
|
||||
daemon.releaseName(oldName)
|
||||
if err = container.ToDisk(); err != nil {
|
||||
return err
|
||||
|
|
|
@ -121,3 +121,15 @@ func (s *DockerSuite) TestRenameContainerWithSameName(c *check.C) {
|
|||
c.Assert(err, checker.NotNil, check.Commentf("Renaming a container with the same name should have failed"))
|
||||
c.Assert(out, checker.Contains, "Renaming a container with the same name", check.Commentf("%v", err))
|
||||
}
|
||||
|
||||
// Test case for #23973
|
||||
func (s *DockerSuite) TestRenameContainerWithLinkedContainer(c *check.C) {
|
||||
testRequires(c, DaemonIsLinux)
|
||||
|
||||
db1, _ := dockerCmd(c, "run", "--name", "db1", "-d", "busybox", "top")
|
||||
dockerCmd(c, "run", "--name", "app1", "-d", "--link", "db1:/mysql", "busybox", "top")
|
||||
dockerCmd(c, "rename", "app1", "app2")
|
||||
out, _, err := dockerCmdWithError("inspect", "--format='{{ .Id }}'", "app2/mysql")
|
||||
c.Assert(err, checker.IsNil)
|
||||
c.Assert(strings.TrimSpace(out), checker.Equals, strings.TrimSpace(db1))
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue