mirror of
https://github.com/0xJacky/nginx-ui.git
synced 2025-05-11 18:35:51 +02:00
fix(docker): get container id by /proc/self/mountinfo
This commit is contained in:
parent
1396cbf096
commit
7b642eb12c
21 changed files with 1208 additions and 1046 deletions
42
internal/docker/container_id.go
Normal file
42
internal/docker/container_id.go
Normal 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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue