You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
30 lines
729 B
Go
30 lines
729 B
Go
2 years ago
|
package container
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"os"
|
||
|
"regexp"
|
||
|
|
||
|
"github.com/containrrr/watchtower/pkg/types"
|
||
|
)
|
||
|
|
||
|
var dockerContainerPattern = regexp.MustCompile(`[0-9]+:.*:/docker/([a-f|0-9]{64})`)
|
||
|
|
||
|
// GetRunningContainerID tries to resolve the current container ID from the current process cgroup information
|
||
|
func GetRunningContainerID() (cid types.ContainerID, err error) {
|
||
|
file, err := os.ReadFile(fmt.Sprintf("/proc/%d/cgroup", os.Getpid()))
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
return getRunningContainerIDFromString(string(file)), nil
|
||
|
}
|
||
|
|
||
|
func getRunningContainerIDFromString(s string) types.ContainerID {
|
||
|
matches := dockerContainerPattern.FindStringSubmatch(s)
|
||
|
if len(matches) < 2 {
|
||
|
return ""
|
||
|
}
|
||
|
return types.ContainerID(matches[1])
|
||
|
}
|