Docker Detector

Detect if Python process is running inside a Docker container

Ask, in one call, whether your Python process is running inside Docker

When it’s useful

Logging, configuration, and feature flags sometimes need to branch on “are we in a container?” Re-implementing cgroup and filesystem probes is tedious and easy to get subtly wrong. This package concentrates that logic behind a single, cache-friendly function, useful in CI images, local compose stacks, and servers where Docker is the runtime.

What you can do

  • Call is_docker() for a boolean answer based on multiple detection paths (as implemented in the library), not a single brittle check.
  • Reuse cached results by default for speed, and force a fresh probe when the environment may have changed mid-process.
  • Ship with zero third-party dependencies beyond the standard library expectations of the package itself.

Limits and fit

Detection answers “Docker-style container” as implemented; unusual sandboxes, partial mounts, or future runtime changes can still surprise any heuristic. If you need proof of a specific orchestrator, combine this with your platform’s own signals. Python 3.8+; install from PyPI (docker-detector). Usage and changelog are in the GitHub repository (conceptually aligned with the is-docker npm utility this project draws from).

Frequently asked questions

What does Docker Detector do?

It tells you whether the current Python process is running inside a Docker container. Call is_docker() for a boolean answer based on multiple detection paths, inspired by the is-docker npm utility.

How do I install and use it?

Requires Python 3.8+. Install with pip install docker-detector (or uv add docker-detector), then from docker_detector import is_docker.

Are results cached?

Yes. Results are cached by default for speed. Pass force_refresh=True when the environment may have changed mid-process and you need a fresh probe.

What are the limits?

It answers Docker-style container detection as implemented. Unusual sandboxes, partial mounts, or future runtime changes can still surprise any heuristic. The package ships with zero third-party dependencies.

Continue exploring