Skip to content

Commit 525f6f8

Browse files
NicolasDoriershyim
authored andcommitted
Fix docker-gen breaking on new docker version (See nginx-proxy#335)
1 parent 91434f6 commit 525f6f8

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

context.go

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ package dockergen
22

33
import (
44
"bufio"
5+
"fmt"
6+
"github.com/fsouza/go-dockerclient"
57
"os"
68
"regexp"
79
"sync"
8-
9-
"github.com/fsouza/go-dockerclient"
1010
)
1111

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

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

164-
if err != nil {
165-
return ""
166-
}
167-
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)