Skip to content

Commit

Permalink
GetCurrentContainerID from /proc/self/mountinfo nginx-proxy#335 + ngi…
Browse files Browse the repository at this point in the history
  • Loading branch information
bugficks committed Feb 28, 2021
1 parent 26dd3bc commit bf31bf1
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 19 deletions.
41 changes: 22 additions & 19 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"regexp"
"sync"

"github.com/fsouza/go-dockerclient"
docker "github.com/fsouza/go-dockerclient"
)

var (
Expand Down Expand Up @@ -161,24 +161,27 @@ type Docker struct {
}

func GetCurrentContainerID() string {
file, err := os.Open("/proc/self/cgroup")
filepaths := []string{"/proc/self/cgroup", "/proc/self/mountinfo"}

if err != nil {
return ""
}
for _, filepath := range filepaths {
file, err := os.Open(filepath)
if err != nil {
continue
}

reader := bufio.NewReader(file)
scanner := bufio.NewScanner(reader)
scanner.Split(bufio.ScanLines)

for scanner.Scan() {
_, lines, err := bufio.ScanLines([]byte(scanner.Text()), true)
if err == nil {
strLines := string(lines)
if id := matchDockerCurrentContainerID(strLines); id != "" {
return id
} else if id := matchECSCurrentContainerID(strLines); id != "" {
return id
reader := bufio.NewReader(file)
scanner := bufio.NewScanner(reader)
scanner.Split(bufio.ScanLines)

for scanner.Scan() {
_, lines, err := bufio.ScanLines([]byte(scanner.Text()), true)
if err == nil {
strLines := string(lines)
if id := matchDockerCurrentContainerID(strLines); id != "" {
return id
} else if id := matchECSCurrentContainerID(strLines); id != "" {
return id
}
}
}
}
Expand All @@ -187,12 +190,12 @@ func GetCurrentContainerID() string {
}

func matchDockerCurrentContainerID(lines string) string {
regex := "/docker[/-]([[:alnum:]]{64})(\\.scope)?$"
regex := "/docker(/containers)?[/-]([[:alnum:]]{64})(/|$)"
re := regexp.MustCompilePOSIX(regex)

if re.MatchString(lines) {
submatches := re.FindStringSubmatch(string(lines))
containerID := submatches[1]
containerID := submatches[len(submatches)-2]

return containerID
}
Expand Down
32 changes: 32 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,37 @@ func TestGetCurrentContainerID_DockerCE(t *testing.T) {
if got, exp := matchDockerCurrentContainerID(cgroup), "18862cabc2e0d24142cf93c46ccb6e070c2ea7b996c81c0311ec0309abcbcdfb"; got != exp {
t.Fatalf("id mismatch: got %v, exp %v", got, exp)
}
}

func TestGetCurrentContainerID_DockerCE_mountinfo(t *testing.T) {
mountinfo :=
`550 209 0:38 / / rw,relatime master:97 - overlay overlay rw,seclabel,lowerdir=/var/lib/docker/overlay2/l/CMLIRGTLVPWATOT2BBGMMWTKPL:/var/lib/docker/overlay2/l/JBJ54L4BQPWTRHZVLNTFAIKIMU:/var/lib/docker/overlay2/l/GKWSGW3V5DTMUG5OV2QF2NDXZM:/var/lib/docker/overlay2/l/KKB7IBA5SXQ6GOHKPC3X2TVPDL,upperdir=/var/lib/docker/overlay2/d3c3d349e227bba0d9f66787a03a5ba8928e6fb8198e787d4d038425afc89fd0/diff,workdir=/var/lib/docker/overlay2/d3c3d349e227bba0d9f66787a03a5ba8928e6fb8198e787d4d038425afc89fd0/work
551 550 0:47 / /proc rw,nosuid,nodev,noexec,relatime - proc proc rw
552 550 0:48 / /dev rw,nosuid - tmpfs tmpfs rw,seclabel,size=65536k,mode=755,inode64
553 552 0:49 / /dev/pts rw,nosuid,noexec,relatime - devpts devpts rw,seclabel,gid=5,mode=620,ptmxmode=666
554 550 0:50 / /sys ro,nosuid,nodev,noexec,relatime - sysfs sysfs ro,seclabel
555 554 0:27 / /sys/fs/cgroup ro,nosuid,nodev,noexec,relatime - cgroup2 cgroup rw,seclabel,nsdelegate
556 552 0:46 / /dev/mqueue rw,nosuid,nodev,noexec,relatime - mqueue mqueue rw,seclabel
557 552 0:51 / /dev/shm rw,nosuid,nodev,noexec,relatime - tmpfs shm rw,seclabel,size=65536k,inode64
558 550 253:1 /var/lib/docker/containers/bc4ed21e220deb8e759970f0853854b868f0157d1b45caab844c5641435553a4/resolv.conf /etc/resolv.conf rw,relatime - xfs /dev/mapper/fedora_tpdachsserver-root rw,seclabel,attr2,inode64,logbufs=8,logbsize=32k,noquota
559 550 253:1 /var/lib/docker/containers/bc4ed21e220deb8e759970f0853854b868f0157d1b45caab844c5641435553a4/hostname /etc/hostname rw,relatime - xfs /dev/mapper/fedora_tpdachsserver-root rw,seclabel,attr2,inode64,logbufs=8,logbsize=32k,noquota
560 550 253:1 /var/lib/docker/containers/bc4ed21e220deb8e759970f0853854b868f0157d1b45caab844c5641435553a4/hosts /etc/hosts rw,relatime - xfs /dev/mapper/fedora_tpdachsserver-root rw,seclabel,attr2,inode64,logbufs=8,logbsize=32k,noquota
210 551 0:47 /bus /proc/bus ro,relatime - proc proc rw
211 551 0:47 /fs /proc/fs ro,relatime - proc proc rw
212 551 0:47 /irq /proc/irq ro,relatime - proc proc rw
264 551 0:47 /sys /proc/sys ro,relatime - proc proc rw
265 551 0:47 /sysrq-trigger /proc/sysrq-trigger ro,relatime - proc proc rw
523 551 0:58 / /proc/asound ro,relatime - tmpfs tmpfs ro,seclabel,inode64
524 551 0:59 / /proc/acpi ro,relatime - tmpfs tmpfs ro,seclabel,inode64
529 551 0:48 /null /proc/kcore rw,nosuid - tmpfs tmpfs rw,seclabel,size=65536k,mode=755,inode64
530 551 0:48 /null /proc/keys rw,nosuid - tmpfs tmpfs rw,seclabel,size=65536k,mode=755,inode64
531 551 0:48 /null /proc/latency_stats rw,nosuid - tmpfs tmpfs rw,seclabel,size=65536k,mode=755,inode64
532 551 0:48 /null /proc/timer_list rw,nosuid - tmpfs tmpfs rw,seclabel,size=65536k,mode=755,inode64
533 551 0:48 /null /proc/sched_debug rw,nosuid - tmpfs tmpfs rw,seclabel,size=65536k,mode=755,inode64
534 551 0:60 / /proc/scsi ro,relatime - tmpfs tmpfs ro,seclabel,inode64
535 554 0:61 / /sys/firmware ro,relatime - tmpfs tmpfs ro,seclabel,inode64`

if got, exp := matchDockerCurrentContainerID(mountinfo), "bc4ed21e220deb8e759970f0853854b868f0157d1b45caab844c5641435553a4"; got != exp {
t.Fatalf("id mismatch: got %v, exp %v", got, exp)
}
}

0 comments on commit bf31bf1

Please sign in to comment.