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
161161func 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
187188func 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