2022-11-01 00:00:00 +01:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"regexp"
|
|
|
|
|
2023-10-04 12:17:38 +02:00
|
|
|
"github.com/nicholas-fedor/watchtower/pkg/types"
|
2022-11-01 00:00:00 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
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])
|
|
|
|
}
|