dockerdetector

package module
v0.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 24, 2022 License: MIT Imports: 10 Imported by: 0

README

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateIDFromDocker

func CreateIDFromDocker() (string, error)

func CreateProtectedFromDockerID

func CreateProtectedFromDockerID(salt string) (string, error)

func IsRunningInContainer

func IsRunningInContainer() (bool, error)

IsRunningInContainer check in cgroup if your are running in a docker container

Types

This section is empty.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL