diff --git a/daemon/delete.go b/daemon/delete.go index aea33c84c8..222f33cba2 100644 --- a/daemon/delete.go +++ b/daemon/delete.go @@ -128,7 +128,7 @@ func (daemon *Daemon) cleanupContainer(container *container.Container, forceRemo return errors.Wrapf(err, "unable to remove filesystem for %s", container.ID) } - daemon.linkIndex.delete(container) + linkNames := daemon.linkIndex.delete(container) selinuxFreeLxcContexts(container.ProcessLabel) daemon.idIndex.Delete(container.ID) daemon.containers.Delete(container.ID) @@ -136,6 +136,9 @@ func (daemon *Daemon) cleanupContainer(container *container.Container, forceRemo if e := daemon.removeMountPoints(container, removeVolume); e != nil { logrus.Error(e) } + for _, name := range linkNames { + daemon.releaseName(name) + } container.SetRemoved() stateCtr.del(container.ID) daemon.LogContainerEvent(container, "destroy") diff --git a/daemon/links.go b/daemon/links.go index 7f691d4f16..219a502766 100644 --- a/daemon/links.go +++ b/daemon/links.go @@ -76,12 +76,16 @@ func (l *linkIndex) parents(child *container.Container) map[string]*container.Co } // delete deletes all link relationships referencing this container -func (l *linkIndex) delete(container *container.Container) { +func (l *linkIndex) delete(container *container.Container) []string { l.mu.Lock() - for _, child := range l.idx[container] { + + var aliases []string + for alias, child := range l.idx[container] { + aliases = append(aliases, alias) delete(l.childIdx[child], container) } delete(l.idx, container) delete(l.childIdx, container) l.mu.Unlock() + return aliases } diff --git a/integration-cli/docker_cli_ps_test.go b/integration-cli/docker_cli_ps_test.go index 98a20f4267..adf0cf48be 100644 --- a/integration-cli/docker_cli_ps_test.go +++ b/integration-cli/docker_cli_ps_test.go @@ -965,3 +965,22 @@ func (s *DockerSuite) TestPsListContainersFilterPorts(c *check.C) { c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), id1) c.Assert(strings.TrimSpace(out), checker.Equals, id2) } + +func (s *DockerSuite) TestPsNotShowLinknamesOfDeletedContainer(c *check.C) { + testRequires(c, DaemonIsLinux) + + dockerCmd(c, "create", "--name=aaa", "busybox", "top") + dockerCmd(c, "create", "--name=bbb", "--link=aaa", "busybox", "top") + + out, _ := dockerCmd(c, "ps", "--no-trunc", "-a", "--format", "{{.Names}}") + lines := strings.Split(strings.TrimSpace(string(out)), "\n") + expected := []string{"bbb", "aaa,bbb/aaa"} + var names []string + names = append(names, lines...) + c.Assert(expected, checker.DeepEquals, names, check.Commentf("Expected array with non-truncated names: %v, got: %v", expected, names)) + + dockerCmd(c, "rm", "bbb") + + out, _ = dockerCmd(c, "ps", "--no-trunc", "-a", "--format", "{{.Names}}") + c.Assert(strings.TrimSpace(out), checker.Equals, "aaa") +}