Skip to content

Commit

Permalink
Merge pull request moby#34109 from yummypeng/rm-link-when-rm-container
Browse files Browse the repository at this point in the history
Bugfix: Remove links when remove container
  • Loading branch information
cpuguy83 authored Aug 15, 2017
2 parents 479cc38 + 600ad5c commit b649834
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
5 changes: 4 additions & 1 deletion daemon/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,17 @@ 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)
daemon.containersReplica.Delete(container)
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")
Expand Down
8 changes: 6 additions & 2 deletions daemon/links.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
19 changes: 19 additions & 0 deletions integration-cli/docker_cli_ps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

0 comments on commit b649834

Please sign in to comment.