5
5
"os"
6
6
"regexp"
7
7
"sync"
8
-
8
+ "fmt"
9
9
"github.com/fsouza/go-dockerclient"
10
10
)
11
11
@@ -159,24 +159,25 @@ type Docker struct {
159
159
}
160
160
161
161
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" }
167
163
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
+ }
180
181
}
181
182
}
182
183
}
@@ -185,7 +186,8 @@ func GetCurrentContainerID() string {
185
186
}
186
187
187
188
func matchDockerCurrentContainerID (lines string ) string {
188
- regex := "/docker[/-]([[:alnum:]]{64})(\\ .scope)?$"
189
+ hostname := os .Getenv ("HOSTNAME" )
190
+ regex := fmt .Sprintf ("(%s[[:alnum:]]{52})" , hostname )
189
191
re := regexp .MustCompilePOSIX (regex )
190
192
191
193
if re .MatchString (lines ) {
0 commit comments