Skip to content

Commit e51c234

Browse files
authored
Merge pull request #336 from btcpayserver/quwn3e
Fix docker-gen breaking on new docker version (See #335)
2 parents faaa781 + 5fb960a commit e51c234

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

context.go

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"os"
66
"regexp"
77
"sync"
8-
8+
"fmt"
99
"github.com/fsouza/go-dockerclient"
1010
)
1111

@@ -159,24 +159,25 @@ type Docker struct {
159159
}
160160

161161
func GetCurrentContainerID() string {
162-
file, err := os.Open("/proc/self/cgroup")
163-
164-
if err != nil {
165-
return ""
166-
}
162+
filepaths := []string{"/proc/self/cgroup", "/proc/self/mountinfo"}
167163

168-
reader := bufio.NewReader(file)
169-
scanner := bufio.NewScanner(reader)
170-
scanner.Split(bufio.ScanLines)
171-
172-
for scanner.Scan() {
173-
_, lines, err := bufio.ScanLines([]byte(scanner.Text()), true)
174-
if err == nil {
175-
strLines := string(lines)
176-
if id := matchDockerCurrentContainerID(strLines); id != "" {
177-
return id
178-
} else if id := matchECSCurrentContainerID(strLines); id != "" {
179-
return id
164+
for _, filepath := range filepaths {
165+
file, err := os.Open(filepath)
166+
if err != nil {
167+
continue
168+
}
169+
reader := bufio.NewReader(file)
170+
scanner := bufio.NewScanner(reader)
171+
scanner.Split(bufio.ScanLines)
172+
for scanner.Scan() {
173+
_, lines, err := bufio.ScanLines([]byte(scanner.Text()), true)
174+
if err == nil {
175+
strLines := string(lines)
176+
if id := matchDockerCurrentContainerID(strLines); id != "" {
177+
return id
178+
} else if id := matchECSCurrentContainerID(strLines); id != "" {
179+
return id
180+
}
180181
}
181182
}
182183
}
@@ -185,7 +186,8 @@ func GetCurrentContainerID() string {
185186
}
186187

187188
func matchDockerCurrentContainerID(lines string) string {
188-
regex := "/docker[/-]([[:alnum:]]{64})(\\.scope)?$"
189+
hostname := os.Getenv("HOSTNAME")
190+
regex := fmt.Sprintf("(%s[[:alnum:]]{52})", hostname)
189191
re := regexp.MustCompilePOSIX(regex)
190192

191193
if re.MatchString(lines) {

0 commit comments

Comments
 (0)