Skip to content

Commit 5f59705

Browse files
committed
Merge pull request #158 from baptistedonaux/master
Add CurrentContainerID into Docker struct
2 parents f125ca9 + a17460b commit 5f59705

File tree

3 files changed

+71
-24
lines changed

3 files changed

+71
-24
lines changed

README.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -272,14 +272,15 @@ type SwarmNode struct {
272272

273273
// Accessible from the root in templates as .Docker
274274
type Docker struct {
275-
Name string
276-
NumContainers int
277-
NumImages int
278-
Version string
279-
ApiVersion string
280-
GoVersion string
281-
OperatingSystem string
282-
Architecture string
275+
Name string
276+
NumContainers int
277+
NumImages int
278+
Version string
279+
ApiVersion string
280+
GoVersion string
281+
OperatingSystem string
282+
Architecture string
283+
CurrentContainerID string
283284
}
284285

285286
// Host environment variables accessible from root in templates as .Env

context.go

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package dockergen
22

33
import (
4+
"bufio"
45
"os"
6+
"regexp"
57
"sync"
68

79
"github.com/fsouza/go-dockerclient"
@@ -29,14 +31,15 @@ func SetServerInfo(d *docker.Env) {
2931
mu.Lock()
3032
defer mu.Unlock()
3133
dockerInfo = Docker{
32-
Name: d.Get("Name"),
33-
NumContainers: d.GetInt("Containers"),
34-
NumImages: d.GetInt("Images"),
35-
Version: dockerEnv.Get("Version"),
36-
ApiVersion: dockerEnv.Get("ApiVersion"),
37-
GoVersion: dockerEnv.Get("GoVersion"),
38-
OperatingSystem: dockerEnv.Get("Os"),
39-
Architecture: dockerEnv.Get("Arch"),
34+
Name: d.Get("Name"),
35+
NumContainers: d.GetInt("Containers"),
36+
NumImages: d.GetInt("Images"),
37+
Version: dockerEnv.Get("Version"),
38+
ApiVersion: dockerEnv.Get("ApiVersion"),
39+
GoVersion: dockerEnv.Get("GoVersion"),
40+
OperatingSystem: dockerEnv.Get("Os"),
41+
Architecture: dockerEnv.Get("Arch"),
42+
CurrentContainerID: GetCurrentContainerID(),
4043
}
4144
}
4245

@@ -139,12 +142,42 @@ type Mount struct {
139142
}
140143

141144
type Docker struct {
142-
Name string
143-
NumContainers int
144-
NumImages int
145-
Version string
146-
ApiVersion string
147-
GoVersion string
148-
OperatingSystem string
149-
Architecture string
145+
Name string
146+
NumContainers int
147+
NumImages int
148+
Version string
149+
ApiVersion string
150+
GoVersion string
151+
OperatingSystem string
152+
Architecture string
153+
CurrentContainerID string
154+
}
155+
156+
func GetCurrentContainerID() string {
157+
file, err := os.Open("/proc/self/cgroup")
158+
159+
if err != nil {
160+
return ""
161+
}
162+
163+
reader := bufio.NewReader(file)
164+
scanner := bufio.NewScanner(reader)
165+
scanner.Split(bufio.ScanLines)
166+
167+
regex := "/docker/([[:alnum:]]{64})$"
168+
re := regexp.MustCompilePOSIX(regex)
169+
170+
for scanner.Scan() {
171+
_, lines, err := bufio.ScanLines([]byte(scanner.Text()), true)
172+
if err == nil {
173+
if re.MatchString(string(lines)) {
174+
submatches := re.FindStringSubmatch(string(lines))
175+
containerID := submatches[1]
176+
177+
return containerID
178+
}
179+
}
180+
}
181+
182+
return ""
150183
}

context_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package dockergen
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestGetCurrentContainerID(t *testing.T) {
8+
currentContainerID := GetCurrentContainerID()
9+
10+
if len(currentContainerID) != 0 && len(currentContainerID) != 64 {
11+
t.Fail()
12+
}
13+
}

0 commit comments

Comments
 (0)