forked from lucaslorentz/caddy-docker-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dockerutils.go
42 lines (36 loc) · 920 Bytes
/
dockerutils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package plugin
import (
"errors"
"io/ioutil"
"os"
"regexp"
"runtime"
)
// DockerUtils is an interface with docker utilities
type DockerUtils interface {
GetCurrentContainerID() (string, error)
}
type dockerUtils struct{}
// CreateDockerUtils creates a new instance of docker utils
func CreateDockerUtils() DockerUtils {
return &dockerUtils{}
}
// GetCurrentContainerID returns the id of the container running this application
func (wrapper *dockerUtils) GetCurrentContainerID() (string, error) {
if runtime.GOOS == "windows" {
return os.Hostname()
}
bytes, err := ioutil.ReadFile("/proc/self/cgroup")
if err != nil {
return "", err
}
if len(bytes) > 0 {
cgroups := string(bytes)
idRegex := regexp.MustCompile("docker/([A-Za-z0-9]+)")
matches := idRegex.FindStringSubmatch(cgroups)
if len(matches) > 1 {
return matches[1], nil
}
}
return "", errors.New("Cannot find container id")
}