Skip to content

Commit dc155e1

Browse files
Add CurrentContainerName field in Docker struct
1 parent 9bfce75 commit dc155e1

File tree

2 files changed

+53
-24
lines changed

2 files changed

+53
-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+
CurrentContainerName string
283284
}
284285

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

context.go

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

33
import (
4+
"io/ioutil"
45
"os"
6+
"regexp"
7+
"strings"
58
"sync"
69

710
"github.com/fsouza/go-dockerclient"
@@ -29,14 +32,16 @@ func SetServerInfo(d *docker.Env) {
2932
mu.Lock()
3033
defer mu.Unlock()
3134
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"),
35+
Name: d.Get("Name"),
36+
NumContainers: d.GetInt("Containers"),
37+
NumImages: d.GetInt("Images"),
38+
Version: dockerEnv.Get("Version"),
39+
ApiVersion: dockerEnv.Get("ApiVersion"),
40+
GoVersion: dockerEnv.Get("GoVersion"),
41+
OperatingSystem: dockerEnv.Get("Os"),
42+
Architecture: dockerEnv.Get("Arch"),
43+
CurrentContainerName: GetCurrentContainerName(),
44+
4045
}
4146
}
4247

@@ -139,12 +144,35 @@ type Mount struct {
139144
}
140145

141146
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
147+
Name string
148+
NumContainers int
149+
NumImages int
150+
Version string
151+
ApiVersion string
152+
GoVersion string
153+
OperatingSystem string
154+
Architecture string
155+
CurrentContainerName string
150156
}
157+
158+
func GetCurrentContainerName() string {
159+
contents, err := ioutil.ReadFile("/proc/self/cgroup")
160+
if err != nil {
161+
println("No /proc/self/cgroup file. Fail to detect current container ID")
162+
return ""
163+
}
164+
165+
lines := strings.Split(string(contents), "\n")
166+
re := regexp.MustCompilePOSIX("^1")
167+
168+
for _, line := range lines {
169+
if re.MatchString(line) == true {
170+
splited := strings.SplitN(line, "/docker-", 2)
171+
splited = strings.SplitN(splited[len(splited) - 1], ".scope", 2)
172+
173+
return splited[0]
174+
}
175+
}
176+
177+
return ""
178+
}

0 commit comments

Comments
 (0)