From 86f9182562cbe16f094f4075c1fe99142f8cbc8c Mon Sep 17 00:00:00 2001 From: jhstianwei <1058211924@qq.com> Date: Mon, 29 Jul 2019 14:11:53 +0800 Subject: [PATCH] remove container and remove cgroup files of container (#6) remove container and remove cgroup files of container --- Dockerfile | 4 ++-- Dockerfile.aarch64 | 4 ++-- Dockerfile.armhf | 4 ++-- Dockerfile.gccgo | 4 ++-- Dockerfile.ppc64le | 4 ++-- Dockerfile.s390x | 4 ++-- Dockerfile.simple | 4 ++-- daemon/delete.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 72 insertions(+), 14 deletions(-) diff --git a/Dockerfile b/Dockerfile index 40ab8ffbd659d..5ee8307e18b60 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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" \ diff --git a/Dockerfile.aarch64 b/Dockerfile.aarch64 index abbb71ab917f4..ca7d2c5eaa159 100644 --- a/Dockerfile.aarch64 +++ b/Dockerfile.aarch64 @@ -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" \ diff --git a/Dockerfile.armhf b/Dockerfile.armhf index a1269c83d9e20..0f23ec8a63e3f 100644 --- a/Dockerfile.armhf +++ b/Dockerfile.armhf @@ -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" \ diff --git a/Dockerfile.gccgo b/Dockerfile.gccgo index 9f79ce45501ea..ed917dfa0a58e 100644 --- a/Dockerfile.gccgo +++ b/Dockerfile.gccgo @@ -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" \ diff --git a/Dockerfile.ppc64le b/Dockerfile.ppc64le index fdce4eb649a3d..bd2db7370857a 100644 --- a/Dockerfile.ppc64le +++ b/Dockerfile.ppc64le @@ -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" \ diff --git a/Dockerfile.s390x b/Dockerfile.s390x index 346afa839356b..16125a3f219d3 100644 --- a/Dockerfile.s390x +++ b/Dockerfile.s390x @@ -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" \ diff --git a/Dockerfile.simple b/Dockerfile.simple index 8aeb6e681c193..f661f38883bf9 100644 --- a/Dockerfile.simple +++ b/Dockerfile.simple @@ -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" \ diff --git a/daemon/delete.go b/daemon/delete.go index ec9d5c5f18e5b..80bdf67094eab 100644 --- a/daemon/delete.go +++ b/daemon/delete.go @@ -5,6 +5,7 @@ import ( "os" "path" "strings" + "time" "github.com/Sirupsen/logrus" "github.com/docker/docker/container" @@ -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 @@ -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 }