This package use cgroup to determine if we run as a docker container. It runs only under linux, windows und macOS will always returning false.
This package can also creates a static ID (Docker ID) and a salted Id (Docker Protected ID) encoded as a hex string. Used cryptographic primitives are SHA256 and HMAC-SHA256.
go get -u github.com/haevg-rz/dockerdetector
package main
import (
"fmt"
"github.com/haevg-rz/dockerdetector"
)
func main() {
isDocker, err := dockerdetector.IsRunningInContainer()
if err != nil {
fmt.Println("Error:", err)
}
fmt.Println("Run in Docker:", isDocker)
if isDocker {
id, err := dockerdetector.CreateIDFromDocker()
if err != nil {
fmt.Println("Error:", err)
}
fmt.Println("Docker ID:", id)
}
if isDocker {
id, err := dockerdetector.CreateProtectedFromDockerID("My Salt")
if err != nil {
fmt.Println("Error:", err)
}
fmt.Println("Docker Protected ID:", id)
}
}
$ docker run -i -t --rm golang
root@110c80b1eb43:/go# go install github.com/haevg-rz/dockerdetector/cmd/dockerdetector@latest
root@110c80b1eb43:/go# dockerdetector
Run in Docker: true
Docker ID: 3ae2bacb803925e4e1be937b8e4609d138abcd6cf61165d9a57a48823107ad56
Docker Protected ID: 4dbeebcbcf17bf073343360ee3db67f7fb31214d661d61a2a3b03abe83c9ac3c