Skip to content

Commit

Permalink
remove container and remove cgroup files of container (moby#6)
Browse files Browse the repository at this point in the history
remove container and remove cgroup files of container
  • Loading branch information
jhstianwei authored and rockuw committed Jul 29, 2019
1 parent f1b6a66 commit 86f9182
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 14 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Expand Up @@ -242,10 +242,10 @@ RUN set -x \
&& rm -rf "$GOPATH"

# Install runc
ENV RUNC_COMMIT 50a19c6ff828c58e5dab13830bd3dacde268afe5
ENV RUNC_COMMIT 1147685483ef383539d55035b95de038c7c068c5
RUN set -x \
&& export GOPATH="$(mktemp -d)" \
&& git clone https://github.com/docker/runc.git "$GOPATH/src/github.com/opencontainers/runc" \
&& git clone https://github.com/aliyun-fc/runc.git "$GOPATH/src/github.com/opencontainers/runc" \
&& cd "$GOPATH/src/github.com/opencontainers/runc" \
&& git checkout -q "$RUNC_COMMIT" \
&& make static BUILDTAGS="seccomp apparmor selinux" \
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile.aarch64
Expand Up @@ -181,10 +181,10 @@ RUN set -x \
&& rm -rf "$GOPATH"

# Install runc
ENV RUNC_COMMIT 50a19c6ff828c58e5dab13830bd3dacde268afe5
ENV RUNC_COMMIT 1147685483ef383539d55035b95de038c7c068c5
RUN set -x \
&& export GOPATH="$(mktemp -d)" \
&& git clone https://github.com/docker/runc.git "$GOPATH/src/github.com/opencontainers/runc" \
&& git clone https://github.com/aliyun-fc/runc.git "$GOPATH/src/github.com/opencontainers/runc" \
&& cd "$GOPATH/src/github.com/opencontainers/runc" \
&& git checkout -q "$RUNC_COMMIT" \
&& make static BUILDTAGS="seccomp apparmor selinux" \
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile.armhf
Expand Up @@ -190,10 +190,10 @@ RUN set -x \
&& rm -rf "$GOPATH"

# Install runc
ENV RUNC_COMMIT 50a19c6ff828c58e5dab13830bd3dacde268afe5
ENV RUNC_COMMIT 1147685483ef383539d55035b95de038c7c068c5
RUN set -x \
&& export GOPATH="$(mktemp -d)" \
&& git clone https://github.com/docker/runc.git "$GOPATH/src/github.com/opencontainers/runc" \
&& git clone https://github.com/aliyun-fc/runc.git "$GOPATH/src/github.com/opencontainers/runc" \
&& cd "$GOPATH/src/github.com/opencontainers/runc" \
&& git checkout -q "$RUNC_COMMIT" \
&& make static BUILDTAGS="seccomp apparmor selinux" \
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile.gccgo
Expand Up @@ -74,10 +74,10 @@ WORKDIR /go/src/github.com/docker/docker
ENV DOCKER_BUILDTAGS apparmor seccomp selinux

# Install runc
ENV RUNC_COMMIT 50a19c6ff828c58e5dab13830bd3dacde268afe5
ENV RUNC_COMMIT 1147685483ef383539d55035b95de038c7c068c5
RUN set -x \
&& export GOPATH="$(mktemp -d)" \
&& git clone https://github.com/docker/runc.git "$GOPATH/src/github.com/opencontainers/runc" \
&& git clone https://github.com/aliyun-fc/runc.git "$GOPATH/src/github.com/opencontainers/runc" \
&& cd "$GOPATH/src/github.com/opencontainers/runc" \
&& git checkout -q "$RUNC_COMMIT" \
&& make static BUILDTAGS="seccomp apparmor selinux" \
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile.ppc64le
Expand Up @@ -195,10 +195,10 @@ RUN set -x \
&& rm -rf "$GOPATH"

# Install runc
ENV RUNC_COMMIT 50a19c6ff828c58e5dab13830bd3dacde268afe5
ENV RUNC_COMMIT 1147685483ef383539d55035b95de038c7c068c5
RUN set -x \
&& export GOPATH="$(mktemp -d)" \
&& git clone https://github.com/docker/runc.git "$GOPATH/src/github.com/opencontainers/runc" \
&& git clone https://github.com/aliyun-fc/runc.git "$GOPATH/src/github.com/opencontainers/runc" \
&& cd "$GOPATH/src/github.com/opencontainers/runc" \
&& git checkout -q "$RUNC_COMMIT" \
&& make static BUILDTAGS="apparmor seccomp selinux" \
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile.s390x
Expand Up @@ -197,10 +197,10 @@ RUN set -x \
&& rm -rf "$GOPATH"

# Install runc
ENV RUNC_COMMIT 50a19c6ff828c58e5dab13830bd3dacde268afe5
ENV RUNC_COMMIT 1147685483ef383539d55035b95de038c7c068c5
RUN set -x \
&& export GOPATH="$(mktemp -d)" \
&& git clone https://github.com/docker/runc.git "$GOPATH/src/github.com/opencontainers/runc" \
&& git clone https://github.com/aliyun-fc/runc.git "$GOPATH/src/github.com/opencontainers/runc" \
&& cd "$GOPATH/src/github.com/opencontainers/runc" \
&& git checkout -q "$RUNC_COMMIT" \
&& make static BUILDTAGS="seccomp apparmor selinux" \
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile.simple
Expand Up @@ -57,10 +57,10 @@ ENV GOPATH /go:/go/src/github.com/docker/docker/vendor
ENV CGO_LDFLAGS -L/lib

# Install runc
ENV RUNC_COMMIT 50a19c6ff828c58e5dab13830bd3dacde268afe5
ENV RUNC_COMMIT 1147685483ef383539d55035b95de038c7c068c5
RUN set -x \
&& export GOPATH="$(mktemp -d)" \
&& git clone https://github.com/docker/runc.git "$GOPATH/src/github.com/opencontainers/runc" \
&& git clone https://github.com/aliyun-fc/runc.git "$GOPATH/src/github.com/opencontainers/runc" \
&& cd "$GOPATH/src/github.com/opencontainers/runc" \
&& git checkout -q "$RUNC_COMMIT" \
&& make static BUILDTAGS="seccomp apparmor selinux" \
Expand Down
58 changes: 58 additions & 0 deletions daemon/delete.go
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"path"
"strings"
"time"

"github.com/Sirupsen/logrus"
"github.com/docker/docker/container"
Expand All @@ -14,6 +15,52 @@ import (
"github.com/docker/engine-api/types"
)

var cgroupFilePaths = map[string]string{"cpu,cpuacct":"/sys/fs/cgroup/cpu,cpuacct/docker/%s",
"net_cls,net_prio":"/sys/fs/cgroup/net_cls,net_prio/docker/%s",
"cpuset":"/sys/fs/cgroup/cpuset/docker/%s",
"freezer":"/sys/fs/cgroup/freezer/docker/%s",
"memory":"/sys/fs/cgroup/memory/docker/%s",
"systemd":"/sys/fs/cgroup/systemd/docker/%s",
"net_cls":"/sys/fs/cgroup/net_cls/docker/%s",
"blkio":"/sys/fs/cgroup/blkio/docker/%s",
"cpu":"/sys/fs/cgroup/cpu/docker/%s",
"cpuacct":"/sys/fs/cgroup/cpuacct/docker/%s",
"devices":"/sys/fs/cgroup/devices/docker/%s",
"hugetlb":"/sys/fs/cgroup/hugetlb/docker/%s",
"net_prio":"/sys/fs/cgroup/net_prio/docker/%s",
"perf_event":"/sys/fs/cgroup/perf_event/docker/%s",
"pids":"/sys/fs/cgroup/pids/docker/%s"}

// removePaths iterates over the provided paths removing them.
// We trying to remove all paths five times with increasing delay between tries.
// If after all there are not removed cgroups - appropriate error will be
// returned.
func removePaths(paths map[string]string) (err error) {
delay := 10 * time.Millisecond
for i := 0; i < 5; i++ {
if i != 0 {
time.Sleep(delay)
delay *= 2
}
for s, p := range paths {
os.RemoveAll(p)
//os.RemoveAll(p)
// TODO: here probably should be logging
_, err := os.Stat(p)
// We need this strange way of checking cgroups existence because
// RemoveAll almost always returns error, even on already removed
// cgroups
if os.IsNotExist(err) {
delete(paths, s)
}
}
if len(paths) == 0 {
return nil
}
}
return fmt.Errorf("Failed to remove paths: %v", paths)
}

// ContainerRm removes the container id from the filesystem. An error
// is returned if the container is not found, or if the remove
// fails. If the remove succeeds, the container name is released, and
Expand Down Expand Up @@ -133,6 +180,17 @@ func (daemon *Daemon) cleanupContainer(container *container.Container, forceRemo
}
}

// destroy cgroup files
cgroupPaths := make(map[string]string)
for s, p := range cgroupFilePaths {
cgroupPaths[s] = fmt.Sprintf(p, container.ID)
}

err = removePaths(cgroupPaths)
if err != nil {
return fmt.Errorf("Fail to destroy cgroups of container %s, err %s", container.ID, err)
}

return nil
}

Expand Down

0 comments on commit 86f9182

Please sign in to comment.