isdocker
A tiny, zero-dependency Rust library that detects whether the current process is running inside a Docker container or a compatible OCI runtime.
What is isdocker?
isdocker exposes a single function:
Call it anywhere in your application to find out whether you are inside a container. That's it.
Why this crate exists
Many applications need to adapt their behaviour depending on their runtime environment — logging format, configuration paths, signal handling, and more. Checking for Docker is a common pattern, but writing the detection logic correctly (handling I/O errors, supporting multiple container runtimes, avoiding false positives) takes more care than it first appears.
isdocker packages that logic into a single well-tested function so you never
have to think about it again.
Installation
Add the crate to your Cargo.toml:
[]
= "0.1"
Usage
use is_docker;
Conditional logging configuration:
use is_docker;
How detection works
On Linux, two checks are performed in sequence. The first positive result short-circuits further checks.
1. /.dockerenv
Docker creates the file /.dockerenv in the root of every container
filesystem. Checking for its existence is fast (a single stat syscall) and
reliable for Docker-managed containers.
2. /proc/self/cgroup
The Linux cgroup virtual filesystem exposes the cgroup hierarchy of the current
process at /proc/self/cgroup. Docker, containerd, and Kubernetes all place
containers in named cgroup paths that include recognisable substrings:
| Runtime / orchestrator | Marker in cgroup path |
|---|---|
| Docker Engine | docker |
| Kubernetes | kubepods |
| containerd | containerd |
If any of these strings appear in the cgroup file, the function returns true.
Non-Linux platforms
On macOS, Windows, and other platforms the function always returns false
without performing any I/O. Container detection is only meaningful on Linux.
Safety guarantees
- Never panics — all I/O is handled with
match; errors silently returnfalse. - No
unsafecode — enforced with#![forbid(unsafe_code)]. - No allocations on the fast path — the
.dockerenvcheck is a single existence test with no heap allocation. - Zero dependencies — nothing beyond
std.
Minimum Supported Rust Version (MSRV)
Rust 1.56 (edition 2021). The crate is tested against stable Rust.
License
MIT — see LICENSE.