Browse code

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

Bugfix: Remove links when remove container

Brian Goff authored on 2017/08/15 22:43:00
Showing 3 changed files
... ...
@@ -128,7 +128,7 @@ func (daemon *Daemon) cleanupContainer(container *container.Container, forceRemo
128 128
 		return errors.Wrapf(err, "unable to remove filesystem for %s", container.ID)
129 129
 	}
130 130
 
131
-	daemon.linkIndex.delete(container)
131
+	linkNames := daemon.linkIndex.delete(container)
132 132
 	selinuxFreeLxcContexts(container.ProcessLabel)
133 133
 	daemon.idIndex.Delete(container.ID)
134 134
 	daemon.containers.Delete(container.ID)
... ...
@@ -136,6 +136,9 @@ func (daemon *Daemon) cleanupContainer(container *container.Container, forceRemo
136 136
 	if e := daemon.removeMountPoints(container, removeVolume); e != nil {
137 137
 		logrus.Error(e)
138 138
 	}
139
+	for _, name := range linkNames {
140
+		daemon.releaseName(name)
141
+	}
139 142
 	container.SetRemoved()
140 143
 	stateCtr.del(container.ID)
141 144
 	daemon.LogContainerEvent(container, "destroy")
... ...
@@ -76,12 +76,16 @@ func (l *linkIndex) parents(child *container.Container) map[string]*container.Co
76 76
 }
77 77
 
78 78
 // delete deletes all link relationships referencing this container
79
-func (l *linkIndex) delete(container *container.Container) {
79
+func (l *linkIndex) delete(container *container.Container) []string {
80 80
 	l.mu.Lock()
81
-	for _, child := range l.idx[container] {
81
+
82
+	var aliases []string
83
+	for alias, child := range l.idx[container] {
84
+		aliases = append(aliases, alias)
82 85
 		delete(l.childIdx[child], container)
83 86
 	}
84 87
 	delete(l.idx, container)
85 88
 	delete(l.childIdx, container)
86 89
 	l.mu.Unlock()
90
+	return aliases
87 91
 }
... ...
@@ -965,3 +965,22 @@ func (s *DockerSuite) TestPsListContainersFilterPorts(c *check.C) {
965 965
 	c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), id1)
966 966
 	c.Assert(strings.TrimSpace(out), checker.Equals, id2)
967 967
 }
968
+
969
+func (s *DockerSuite) TestPsNotShowLinknamesOfDeletedContainer(c *check.C) {
970
+	testRequires(c, DaemonIsLinux)
971
+
972
+	dockerCmd(c, "create", "--name=aaa", "busybox", "top")
973
+	dockerCmd(c, "create", "--name=bbb", "--link=aaa", "busybox", "top")
974
+
975
+	out, _ := dockerCmd(c, "ps", "--no-trunc", "-a", "--format", "{{.Names}}")
976
+	lines := strings.Split(strings.TrimSpace(string(out)), "\n")
977
+	expected := []string{"bbb", "aaa,bbb/aaa"}
978
+	var names []string
979
+	names = append(names, lines...)
980
+	c.Assert(expected, checker.DeepEquals, names, check.Commentf("Expected array with non-truncated names: %v, got: %v", expected, names))
981
+
982
+	dockerCmd(c, "rm", "bbb")
983
+
984
+	out, _ = dockerCmd(c, "ps", "--no-trunc", "-a", "--format", "{{.Names}}")
985
+	c.Assert(strings.TrimSpace(out), checker.Equals, "aaa")
986
+}