Skip to content

Commit e14a2d4

Browse files
committed
support docker 1.2 in GetFromDocker
1 parent be82404 commit e14a2d4

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

netns_linux.go

+16-12
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ func getThisCgroup(cgroupType string) (string, error) {
135135
// borrowed from docker/utils/utils.go
136136
// modified to only return the first pid
137137
// modified to glob with id
138+
// modified to search for newer docker containers
138139
func getPidForContainer(id string) (int, error) {
139140
pid := 0
140141

@@ -153,26 +154,29 @@ func getPidForContainer(id string) (int, error) {
153154

154155
id += "*"
155156

156-
filename := filepath.Join(cgroupRoot, cgroupThis, id, "tasks")
157-
filenames, _ := filepath.Glob(filename)
158-
if len(filenames) > 1 {
159-
return pid, fmt.Errorf("Ambiguous id supplied: %v", filenames)
160-
} else if len(filenames) == 1 {
161-
filename = filenames[0]
162-
}
163-
if _, err := os.Stat(filename); os.IsNotExist(err) {
157+
attempts := []string{
158+
filepath.Join(cgroupRoot, cgroupThis, id, "tasks"),
164159
// With more recent lxc versions use, cgroup will be in lxc/
165-
filename = filepath.Join(cgroupRoot, cgroupThis, "lxc", id, "tasks")
166-
filenames, _ = filepath.Glob(filename)
160+
filepath.Join(cgroupRoot, cgroupThis, "lxc", id, "tasks"),
161+
// With more recent dockee, cgroup will be in docker/
162+
filepath.Join(cgroupRoot, cgroupThis, "docker", id, "tasks"),
163+
}
164+
165+
var filename string
166+
for _, attempt := range attempts {
167+
filenames, _ := filepath.Glob(attempt)
167168
if len(filenames) > 1 {
168169
return pid, fmt.Errorf("Ambiguous id supplied: %v", filenames)
169170
} else if len(filenames) == 1 {
170171
filename = filenames[0]
171-
} else {
172-
return pid, fmt.Errorf("Unable to find container: %v", id[:len(id)-1])
172+
break;
173173
}
174174
}
175175

176+
if filename == "" {
177+
return pid, fmt.Errorf("Unable to find container: %v", id[:len(id)-1])
178+
}
179+
176180
output, err := ioutil.ReadFile(filename)
177181
if err != nil {
178182
return pid, err

0 commit comments

Comments
 (0)