Expand description
§isdocker
A tiny, zero-dependency library that detects whether the current process is running inside a Docker container (or a compatible container runtime such as Podman, containerd, or Kubernetes).
§Detection strategy
On Linux, five checks are performed in order. The first conclusive result short-circuits further checks.
| Step | Check | Indicates container when… |
|---|---|---|
| 1 | ISDOCKER env var | "1", "true", or "yes" (case-insensitive) |
| 2 | /.dockerenv exists | file is present (Docker) |
| 3 | /run/.containerenv exists | file is present (Podman) |
| 4 | /proc/self/mountinfo contents | contains Docker or Podman overlay paths |
| 5 | /proc/self/cgroup contents | contains docker, kubepods, or containerd |
§Environment variable override
Setting ISDOCKER=1 (or true / yes) forces the function to return
true. Setting ISDOCKER=0 (or false / no) forces it to return
false. Any other value (or absence) falls through to filesystem checks.
This is the recommended approach for production systems that need reliable, testable environment detection rather than heuristic-based auto-detection.
§Known limitations
The filesystem checks are heuristics, not guarantees:
- cgroupv2 — On modern Linux with the unified cgroup hierarchy,
/proc/self/cgroupcontains only0::/with no runtime markers. The mountinfo check (step 4) mitigates this, but the env var override is the most reliable option. - Docker BuildKit —
/.dockerenvis not created duringdocker buildwith BuildKit/buildx. - Podman +
/runvolume mount —/run/.containerenvis not created when a volume is mounted over/run. - Non-Linux platforms — always returns
falsewithout performing any I/O.
§Safety guarantees
- Never panics.
- Returns
falseon any I/O error rather than propagating errors. - No heap allocations on the fast path (
.dockerenvcheck). #![forbid(unsafe_code)]— no unsafe code anywhere.- Zero dependencies beyond
std.
§Example
use isdocker::is_docker;
if is_docker() {
println!("Running inside a container");
} else {
println!("Not running inside a container");
}Functions§
- is_
container - Alias for
is_docker. - is_
docker - Returns
trueif the current process appears to be running inside a Docker container (or a compatible OCI container runtime such as Podman, containerd, or a Kubernetes pod), andfalseotherwise.