1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Merge pull request #34109 from yummypeng/rm-link-when-rm-container

Bugfix: Remove links when remove container
This commit is contained in:
Brian Goff 2017-08-15 09:43:00 -04:00 committed by GitHub
commit b6498340b2
3 changed files with 29 additions and 3 deletions

View file

@ -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")

View file

@ -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
}

View file

@ -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")
}