fix(docker): get container id by /proc/self/mountinfo

This commit is contained in:
Jacky 2025-05-03 08:51:03 +00:00
parent 1396cbf096
commit 7b642eb12c
No known key found for this signature in database
GPG key ID: 215C21B10DF38B4D
21 changed files with 1208 additions and 1046 deletions

View file

@ -0,0 +1,42 @@
package docker
import (
"bufio"
"os"
"regexp"
"strings"
)
// GetContainerID retrieves the Docker container ID by parsing /proc/self/mountinfo
func GetContainerID() (string, error) {
// Open the mountinfo file
file, err := os.Open("/proc/self/mountinfo")
if err != nil {
return "", err
}
defer file.Close()
// Regular expression to extract container ID from paths like:
// /var/lib/docker/containers/bd4bd482f7e28566389fe7e4ce6b168e93b372c3fc18091c37923588664ca950/resolv.conf
containerIDPattern := regexp.MustCompile(`/var/lib/docker/containers/([a-f0-9]{64})/`)
// Scan the file line by line
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
// Look for container ID in the line
if strings.Contains(line, "/var/lib/docker/containers/") {
matches := containerIDPattern.FindStringSubmatch(line)
if len(matches) >= 2 {
return matches[1], nil
}
}
}
if err := scanner.Err(); err != nil {
return "", err
}
return "", os.ErrNotExist
}