Skip to content

Fix docker-gen breaking on new docker version (See #335) #336

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 3, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NicolasDorier @jwilder this breaks the tests:

--- FAIL: TestGetCurrentContainerID_DockerCE (0.00s)
    context_test.go:49: id mismatch: got 18862cabc2e0d24142cf93c46ccb6e070c2ea7b996c81c0311ec, exp 18862cabc2e0d24142cf93c46ccb6e070c2ea7b996c81c0311ec0309abcbcdfb
FAIL
exit status 1
FAIL    github.com/jwilder/docker-gen   0.596s

re := regexp.MustCompilePOSIX(regex)

if re.MatchString(lines) {
Expand Down