go.undefinedlabs.com/scopeagent@v0.4.2/agent/container.go (about)

     1  package agent
     2  
     3  import (
     4  	"io/ioutil"
     5  	"regexp"
     6  	"runtime"
     7  	"sync"
     8  )
     9  
    10  var (
    11  	runningInContainerOnce sync.Once
    12  	runningInContainer     bool
    13  	containerRegex         = regexp.MustCompile(`(?m)\/docker\/|\/ecs\/|\/docker-|\/kubepods\/|\/actions_job\/|\/lxc\/`)
    14  )
    15  
    16  // gets if the current process is running inside a container
    17  func isRunningInContainer() bool {
    18  	runningInContainerOnce.Do(func() {
    19  		if runtime.GOOS != "linux" {
    20  			runningInContainer = false
    21  			return
    22  		}
    23  		content, err := ioutil.ReadFile("/proc/1/cgroup")
    24  		if err != nil {
    25  			runningInContainer = false
    26  			return
    27  		}
    28  		runningInContainer = containerRegex.Match(content)
    29  	})
    30  	return runningInContainer
    31  }