Skip to content

Commit bf31bf1

Browse files
author
bugficks
committed
GetCurrentContainerID from /proc/self/mountinfo nginx-proxy#335 + nginx-proxy#336
1 parent 26dd3bc commit bf31bf1

File tree

2 files changed

+54
-19
lines changed

2 files changed

+54
-19
lines changed

context.go

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"regexp"
77
"sync"
88

9-
"github.com/fsouza/go-dockerclient"
9+
docker "github.com/fsouza/go-dockerclient"
1010
)
1111

1212
var (
@@ -161,24 +161,27 @@ type Docker struct {
161161
}
162162

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

166-
if err != nil {
167-
return ""
168-
}
166+
for _, filepath := range filepaths {
167+
file, err := os.Open(filepath)
168+
if err != nil {
169+
continue
170+
}
169171

170-
reader := bufio.NewReader(file)
171-
scanner := bufio.NewScanner(reader)
172-
scanner.Split(bufio.ScanLines)
173-
174-
for scanner.Scan() {
175-
_, lines, err := bufio.ScanLines([]byte(scanner.Text()), true)
176-
if err == nil {
177-
strLines := string(lines)
178-
if id := matchDockerCurrentContainerID(strLines); id != "" {
179-
return id
180-
} else if id := matchECSCurrentContainerID(strLines); id != "" {
181-
return id
172+
reader := bufio.NewReader(file)
173+
scanner := bufio.NewScanner(reader)
174+
scanner.Split(bufio.ScanLines)
175+
176+
for scanner.Scan() {
177+
_, lines, err := bufio.ScanLines([]byte(scanner.Text()), true)
178+
if err == nil {
179+
strLines := string(lines)
180+
if id := matchDockerCurrentContainerID(strLines); id != "" {
181+
return id
182+
} else if id := matchECSCurrentContainerID(strLines); id != "" {
183+
return id
184+
}
182185
}
183186
}
184187
}
@@ -187,12 +190,12 @@ func GetCurrentContainerID() string {
187190
}
188191

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

193196
if re.MatchString(lines) {
194197
submatches := re.FindStringSubmatch(string(lines))
195-
containerID := submatches[1]
198+
containerID := submatches[len(submatches)-2]
196199

197200
return containerID
198201
}

context_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,37 @@ func TestGetCurrentContainerID_DockerCE(t *testing.T) {
4848
if got, exp := matchDockerCurrentContainerID(cgroup), "18862cabc2e0d24142cf93c46ccb6e070c2ea7b996c81c0311ec0309abcbcdfb"; got != exp {
4949
t.Fatalf("id mismatch: got %v, exp %v", got, exp)
5050
}
51+
}
52+
53+
func TestGetCurrentContainerID_DockerCE_mountinfo(t *testing.T) {
54+
mountinfo :=
55+
`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
56+
551 550 0:47 / /proc rw,nosuid,nodev,noexec,relatime - proc proc rw
57+
552 550 0:48 / /dev rw,nosuid - tmpfs tmpfs rw,seclabel,size=65536k,mode=755,inode64
58+
553 552 0:49 / /dev/pts rw,nosuid,noexec,relatime - devpts devpts rw,seclabel,gid=5,mode=620,ptmxmode=666
59+
554 550 0:50 / /sys ro,nosuid,nodev,noexec,relatime - sysfs sysfs ro,seclabel
60+
555 554 0:27 / /sys/fs/cgroup ro,nosuid,nodev,noexec,relatime - cgroup2 cgroup rw,seclabel,nsdelegate
61+
556 552 0:46 / /dev/mqueue rw,nosuid,nodev,noexec,relatime - mqueue mqueue rw,seclabel
62+
557 552 0:51 / /dev/shm rw,nosuid,nodev,noexec,relatime - tmpfs shm rw,seclabel,size=65536k,inode64
63+
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
64+
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
65+
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
66+
210 551 0:47 /bus /proc/bus ro,relatime - proc proc rw
67+
211 551 0:47 /fs /proc/fs ro,relatime - proc proc rw
68+
212 551 0:47 /irq /proc/irq ro,relatime - proc proc rw
69+
264 551 0:47 /sys /proc/sys ro,relatime - proc proc rw
70+
265 551 0:47 /sysrq-trigger /proc/sysrq-trigger ro,relatime - proc proc rw
71+
523 551 0:58 / /proc/asound ro,relatime - tmpfs tmpfs ro,seclabel,inode64
72+
524 551 0:59 / /proc/acpi ro,relatime - tmpfs tmpfs ro,seclabel,inode64
73+
529 551 0:48 /null /proc/kcore rw,nosuid - tmpfs tmpfs rw,seclabel,size=65536k,mode=755,inode64
74+
530 551 0:48 /null /proc/keys rw,nosuid - tmpfs tmpfs rw,seclabel,size=65536k,mode=755,inode64
75+
531 551 0:48 /null /proc/latency_stats rw,nosuid - tmpfs tmpfs rw,seclabel,size=65536k,mode=755,inode64
76+
532 551 0:48 /null /proc/timer_list rw,nosuid - tmpfs tmpfs rw,seclabel,size=65536k,mode=755,inode64
77+
533 551 0:48 /null /proc/sched_debug rw,nosuid - tmpfs tmpfs rw,seclabel,size=65536k,mode=755,inode64
78+
534 551 0:60 / /proc/scsi ro,relatime - tmpfs tmpfs ro,seclabel,inode64
79+
535 554 0:61 / /sys/firmware ro,relatime - tmpfs tmpfs ro,seclabel,inode64`
5180

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

0 commit comments

Comments
 (0)