ysskrishna logoysskrishna

Container Detect

One Python call to learn if your process is inside Docker, Podman, Kubernetes, or another Linux container.

Built with
PyPI Packages
DevOps & Infrastructure
Python
uv
PyPI
GitHub Actions
Pytest
Docker
Kubernetes
Details
FreeMIT licenseCreated February 10, 2026Updated November 12, 2026
Share this product

Your config should not depend on where the image last ran.

You ship the same Python service to a laptop, a CI worker, and a cluster. Somewhere along the way you need a honest answer: are we inside a container right now? A Docker-only check misreads Podman and many Kubernetes layouts. A custom env var you baked into one Dockerfile disappears on the next runtime. You end up pasting snippets from three forum threads and hoping they still match cgroup v2.

Container Detect gives you one function that aims for broad container detection behind a single entry point.

Know the runtime before you branch the config.

What you get

  • One answer across runtimes. The docs call out Docker, Podman, Kubernetes, containerd, LXC, CRI-O, AWS ECS, and more container runtimes.
  • Several signals, not one brittle trick. Multiple detection methods combine cgroup reads, marker files, mount patterns, and environment variables so one missing file does not decide everything.
  • Cached by default. Cached results keep repeated checks cheap; pass force_refresh when the environment just changed and you need a fresh read.
  • Fails quietly. Graceful error handling means a filesystem probe that fails does not crash your process.
  • Nothing else to install. Zero dependencies, minimal overhead, Python 3.8 or newer.
  • Free and open source. MIT licensed, published on PyPI.

Picture your Monday

An illustrative case: your logging setup writes verbose traces on a developer laptop but stays quiet inside production images. At startup the app asks whether it is inside a container and picks the profile. The same branch works whether the image landed on Docker Desktop, a Podman build farm, or a Kubernetes node, as long as the process runs on Linux.

Ready to try it?

Need a narrower Docker-only check? Docker Detector covers the classic Docker case on the same site.

Frequently asked questions

Why not just check for a .dockerenv file myself?

You can, and it works for plenty of Docker setups. The changelog still lists that marker alongside Podman files, cgroup keywords, and Kubernetes environment variables because no single signal covers every runtime. Container Detect runs those checks together and returns one inside-or-not answer.

How do I detect if my Python app is running in a Docker container?

Install container-detect from PyPI, import is_inside_container from container_detect, and call it where you branch logging or paths. The README positions the library for multiple container types, with Docker called out explicitly in the supported list.

How can a Python app tell it is inside a Kubernetes pod?

The implementation checks Kubernetes-oriented environment variables such as KUBERNETES_SERVICE_HOST, along with cgroup keywords and other container markers described in the changelog. You get whether the process appears inside a container, not cluster name or version metadata.

Does it detect Podman, not only Docker?

Yes. The README lists Podman in supported container types, and the changelog documents Podman marker files and cgroup patterns. That is usually the gap when you only copy a Docker forum answer.

Is it free?

Yes. It is open source under the MIT license.

Does it work on macOS or Windows?

No for native hosts. The changelog describes Linux-only detection and Linux platform support, reading paths such as proc cgroups and container marker files. Use it on Linux runtimes and containers, including Linux images on other hosts.

How is this different from Docker Detector?

Docker Detector answers whether Python is running inside Docker. Container Detect widens the question to multiple container types behind one is_inside_container entry point, as the GitHub description and README both state.

Continue exploring

ProductPyPIDocker Detector

Docker Detector

One call tells your Python app whether it is running inside Docker.
DevOps & Infrastructure
PyPI Packages
Python
uv
ProductPyPIOSC 7501

OSC 7501

Tell your terminal when your CLI is working, blocked, finished, or failed.
DevOps & Infrastructure
PyPI Packages
Python
Python CLI
BlogCover image for OSC 7501 in Python: Tell the Terminal When Your CLI Is Working, Blocked or Done

OSC 7501 in Python: Tell the Terminal When Your CLI Is Working, Blocked or Done

Python
Python CLI
+1
3 min read
ProductPyPISocial Links

Social Links

Users paste messy profile links. One Python library turns them into clean, comparable URLs.
PyPI Packages
Python
uv
PyPI
ProductPyPINested Utils

Nested Utils

Deep JSON and config paths, one string, no guard maze.
PyPI Packages
Python
uv
PyPI
BlogCover image for caffeinate for Windows: Keep a Locked-Down Laptop Awake Without Installing Anything

caffeinate for Windows: Keep a Locked-Down Laptop Awake Without Installing Anything

DevOps & Infrastructure
PowerShell
4 min read