From 102566032ae3b5c829a8096ab2524ade9edf46d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?nils=20m=C3=A5s=C3=A9n?= Date: Wed, 7 Sep 2022 14:59:26 +0200 Subject: [PATCH] add GetRunningContainerID --- pkg/container/cgroup_id.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 pkg/container/cgroup_id.go diff --git a/pkg/container/cgroup_id.go b/pkg/container/cgroup_id.go new file mode 100644 index 0000000..999026f --- /dev/null +++ b/pkg/container/cgroup_id.go @@ -0,0 +1,24 @@ +package container + +import ( + "fmt" + "os" + "regexp" + + "github.com/containrrr/watchtower/pkg/types" +) + +var dockerContainerPattern = regexp.MustCompile(`[0-9]+:.*:/docker/([a-f|0-9]{64})`) + +func GetRunningContainerID() (cid types.ContainerID, err error) { + file, err := os.ReadFile(fmt.Sprintf("/proc/%d/cgroup", os.Getpid())) + if err != nil { + return + } + + matches := dockerContainerPattern.FindStringSubmatch(string(file)) + if len(matches) < 2 { + return "", nil + } + return types.ContainerID(matches[1]), nil +}