Container Detect

Detect if Python process is running inside a container (Docker, Podman, Kubernetes)

Detect container runtimes, not only Docker, from your Python process

When it’s useful

The same service might run under Docker, Podman, Kubernetes, containerd, or another OCI-style environment. If you only check for Docker, you misclassify everything else. This library aims for broad container detection behind one entry point, with optional cache refresh when you cannot afford stale answers.

What you can do

  • Call is_inside_container() to branch logging, paths, or behavior when the process appears to be inside a supported container style.
  • Cover the runtimes called out in the project docs, including Docker, Podman, Kubernetes, containerd, LXC, CRI-O, AWS ECS, and related paths the implementation tests against.
  • Benefit from multiple detection strategies and graceful error handling so a failed probe does not take down your app.
  • Cache by default and bypass the cache when you need a fresh read after environment changes.

Limits and fit

Like any environment probe, results depend on what the host exposes; new runtimes or hardened images may need updates upstream. It tells you “inside a container per heuristics,” not “which cloud account” or “which orchestrator version.” Python 3.8+; install from PyPI (container-detect). Details match the GitHub repository, inspired by the is-inside-container npm package.

Frequently asked questions

How is Container Detect different from Docker-only checks?

It aims for broad container detection behind is_inside_container(), covering runtimes called out in the docs such as Docker, Podman, Kubernetes, containerd, LXC, CRI-O, and AWS ECS, not only Docker.

How do I install and use it?

Requires Python 3.8+. Install with pip install container-detect (or uv add container-detect), then from container_detect import is_inside_container.

Does it cache results?

Yes. Caching is on by default. Pass force_refresh=True when you need a fresh read after environment changes. Multiple detection strategies and graceful error handling keep a failed probe from taking down your app.

What will it not tell me?

It reports whether the process appears inside a container per heuristics, not which cloud account or orchestrator version. New runtimes or hardened images may need upstream updates.

Continue exploring