Steps to reproduce:
```
# docker run -tid --name aaa ubuntu
57bfd00ac5559f72eec8c1b32a01fe38427d66687940f74611e65137414f0ada
# docker run -tid --name bbb --link aaa ubuntu
23ad18362950f39b638206ab4d1885fd4f50cbd1d16aac9cab8e97e0c8363471
# docker ps --no-trunc
CONTAINER ID IMAGE
COMMAND CREATED STATUS PORTS
NAMES
23ad18362950f39b638206ab4d1885fd4f50cbd1d16aac9cab8e97e0c8363471
ubuntu "/bin/bash" 4 seconds ago Up 3 seconds
bbb
57bfd00ac5559f72eec8c1b32a01fe38427d66687940f74611e65137414f0ada
ubuntu "/bin/bash" 14 seconds ago Up 14
seconds aaa,bbb/aaa
# docker rm -f bbb
bbb
# docker ps --no-trunc
CONTAINER ID IMAGE
COMMAND CREATED STATUS PORTS
NAMES
57bfd00ac5559f72eec8c1b32a01fe38427d66687940f74611e65137414f0ada
ubuntu "/bin/bash" 29 seconds ago Up 28
seconds aaa,bbb/aaa
# docker rm --link bbb/aaa
Error response from daemon: Cannot get parent /bbb for name /bbb/aaa
```
When we rm container `bbb`, we can still see `bbb/aaa` in `docker ps
--no-trunc`. And this link cannot be deleted since container `bbb` has
already been removed.
We should remove links of a container when it is deleted.
Signed-off-by: Yuanhong Peng <pengyuanhong@huawei.com>
| ... | ... |
@@ -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 |
+} |