Skip to content

haevg-rz/dockerdetector

Repository files navigation

dockerdetector

Go codecov Quality Gate Status Go Report Card Go Doc Known Vulnerabilities

Intro

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.

Usage

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)
	}
}

Run in docker

$ 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