In-Container NPM Package

LoryPelli1 pts0 comments

GitHub - lorypelli/in-container · GitHub

/" data-turbo-transient="true" />

Skip to content

Type / to search

Sign in<br>Sign upAppearance settings

You signed in with another tab or window. Reload to refresh your session.<br>You signed out in another tab or window. Reload to refresh your session.<br>You switched accounts on another tab or window. Reload to refresh your session.

Dismiss alert

{{ message }}

lorypelli

in-container

Public

Notifications<br>You must be signed in to change notification settings

Fork

Star

main

BranchesTags

Go to file

CodeOpen more actions menu

Folders and files<br>NameNameLast commit message<br>Last commit date<br>Latest commit

History<br>17 Commits<br>17 Commits

src

src

.gitignore

.gitignore

.prettierignore

.prettierignore

.prettierrc

.prettierrc

LICENSE.md

LICENSE.md

README.md

README.md

package.json

package.json

pnpm-lock.yaml

pnpm-lock.yaml

pnpm-workspace.yaml

pnpm-workspace.yaml

tsconfig.json

tsconfig.json

tsdown.config.ts

tsdown.config.ts

View all files

Repository files navigation

in-container

Check if the current process is running inside a container (Docker, Podman).

Zero dependencies

Usable as a library or as a CLI

Both async and sync flavors

Resolves/returns false (never throws) on platforms without container-specific paths, e.g. Windows or macOS hosts

Requirements

Node.js >=14.18.0 to use as a library.

The CLI additionally requires >=16.17.0 or >=18.3.0, since it<br>relies on parseArgs from node:util. On older versions it exits 2 with<br>an explanatory error instead of failing silently.

Works from both ESM (import) and CommonJS (require).

Install

npm install in-container

Usage

import { inContainer } from 'in-container/async';

if (await inContainer()) {<br>console.log('Running inside a container');

import { inContainer } from 'in-container/sync';

if (inContainer()) {<br>console.log('Running inside a container');

The package root (in-container) re-exports both flavors, suffixed Async/Sync:

import { inContainerAsync, inContainerSync } from 'in-container';

The same applies to inDocker/inDockerAsync/inDockerSync and<br>inPodman/inPodmanAsync/inPodmanSync.

API

inDocker()

Whether the current process appears to be running inside a Docker container.<br>Detected via /.dockerenv, a docker cgroup segment in /proc/self/cgroup,<br>or a /docker/containers/ mount in /proc/self/mountinfo.

inPodman()

Whether the current process appears to be running inside a Podman container.<br>Detected via /run/.containerenv or a libpod/podman cgroup segment in<br>/proc/self/cgroup.

inContainer()

Equivalent to inDocker() || inPodman() (or the awaited equivalent for the<br>async version).

CLI

npx in-container

Usage: in-container [options]

Options:<br>-q, --quiet Only set the exit code<br>-h, --help Print this help message<br>-v, --version Print the version number

Exit code:<br>0 running inside a container<br>1 not running inside a container<br>2 invalid usage

By default it prints all three results as JSON:

$ in-container<br>{"container":true,"docker":true,"podman":false}

Use --quiet in scripts that only care about the exit code:

if in-container --quiet; then<br>echo 'Running inside a container'<br>fi

Limitations

Container detection is a heuristic, not a guarantee:

Only Docker and Podman are detected — other runtimes (containerd, CRI-O,<br>LXC, plain namespaces) usually leave none of these signals, so many<br>Kubernetes pods will report false.

With Docker Desktop on macOS/Windows, your process runs on the host, not in<br>the Linux VM, so the answer is correctly false.

The signals are forgeable — do not use this as a security boundary.

Development

pnpm install<br>pnpm build # bundle src/ to dist/ with tsdown, and emit the type declarations<br>pnpm format # prettier

License

MIT

About<br>npmjs.com/in-container<br>Resources<br>Readme<br>MIT license<br>Activity<br>Stars<br>1 star<br>Watchers<br>0 watching<br>Forks<br>0 forks<br>Report repository

Contributors

Languages

You can’t perform that action at this time.

container running inside pnpm docker json

Related Articles