Remove links when remove container

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>
This commit is contained in:
Yuanhong Peng 2017-07-14 17:25:09 +08:00
parent 08f7cf0526
commit 600ad5c1b7
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")
}