mirror of
https://github.com/containrrr/watchtower.git
synced 2026-01-10 02:58:50 +01:00
add GetRunningContainerID
This commit is contained in:
parent
626bd547e9
commit
102566032a
1 changed files with 24 additions and 0 deletions
24
pkg/container/cgroup_id.go
Normal file
24
pkg/container/cgroup_id.go
Normal file
|
|
@ -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
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue