env_inspector
A minimal Rust crate to detect whether your application is running in a container, a virtual machine, or on bare metal (no container/VM).
Features
- Detect common container environments (Docker, Kubernetes, Podman)
- Identify hypervisor flag in CPU info
- Inspect DMI fields to recognize VM vendors (KVM, VMware, VirtualBox, Hyper-V, Xen, AWS, GCP)
- No external dependencies or network calls
Installation
Add to your Cargo.toml:
[]
= "0.1.0"
Then fetch the crate:
Usage
Call the detect_scope function to get an EnvScope enum:
use ;
Running Tests
Execute the built‑in unit tests:
CLI Example
Optionally, include a small binary in src/bin/inspect.rs:
use detect_scope;
Build and run it:
The test code (src/bin/inspect.rs) should print: BareMetal.
Running as a Docker container
Build the image and run a container:
cargo build --release
docker build -t env-inspector .
docker run --rm env-inspector
You should see this message:
Container(
"docker",
)
If you run it with podman it can detect it:
podman pull docker-daemon:env-inspector:latest
podman run --rm docker-daemon:env-inspector:latest
Container(
"podman",
)
Running in Kubernetes
-
Build and push the Docker image:
# Build locally # Push to your registry (Docker Hub, ECR, GCR, etc.) -
Create a Pod manifest (
k8s/pod.yaml):apiVersion: v1 kind: Pod metadata: name: env-inspector-test spec: containers: - name: inspector image: your-registry/env-inspector:latest command: restartPolicy: Never -
Deploy to your cluster:
-
Check the logs:
You should see output indicating kubernetes, confirming that the code correctly detected the environment inside the cluster.
Running inside a VM Hypervisor
You need some hypervisor like VirtualBox. If you have vagrant then it is easy to run it with the supplied Vagrantfile:
vagrant up
default: Vm(
default: "hypervisor",
default: )
vagrant destroy -f
Links
- Repository: https://github.com/cleuton/rustingcrab/tree/main/code_samples/env_inspector
- Issue Tracker: https://github.com/cleuton/rustingcrab/issues
- License: MIT/Apache-2.0