Skip to content

Commit

Permalink
Fix docker-gen breaking on new docker version (See nginx-proxy#335)
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasDorier committed Jan 21, 2021
1 parent 4edc190 commit 5fb960a
Showing 1 changed file with 21 additions and 19 deletions.
40 changes: 21 additions & 19 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os"
"regexp"
"sync"

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

Expand Down Expand Up @@ -159,24 +159,25 @@ type Docker struct {
}

func GetCurrentContainerID() string {
file, err := os.Open("/proc/self/cgroup")

if err != nil {
return ""
}
filepaths := []string{"/proc/self/cgroup", "/proc/self/mountinfo"}

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
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
}
}
}
}
Expand All @@ -185,7 +186,8 @@ func GetCurrentContainerID() string {
}

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

if re.MatchString(lines) {
Expand Down

0 comments on commit 5fb960a

Please sign in to comment.