package docker

import (
	"bytes"
	"os/exec"
)

var (
	_dockerId string
)

func SelfContainerID() (string, error) {
	if _dockerId != "" {
		return _dockerId, nil
	}
	if buf, err := exec.Command("/bin/sh", "-c", "cat /proc/self/cgroup | grep -o -e \"docker/.*\"| head -n 1 |sed \"s/docker\\/\\(.*\\)/\\\\1/\"").CombinedOutput(); err != nil {
		return "", err
	} else {
		_dockerId = string(bytes.TrimSpace(buf))
		return _dockerId, nil
	}
}
