isDocker (utils/is-docker.ts)

zenstack · packages/cli/src/utils/is-docker.ts · typescript · MIT
infra, Infra / DevOps

Utility to detect if running inside Docker container in Node.js.

// Source: https://github.com/zenstackhq/zenstack | License: MIT | Copyright 2025 ZenStack
// Provided via justclaw.it.com; keep this notice with the code.
// Copied over from https://github.com/sindresorhus/is-docker for CJS compatibility

import fs from 'node:fs';

let isDockerCached: boolean | undefined;

function hasDockerEnv() {
    try {
        fs.statSync('/.dockerenv');
        return true;
    } catch {
        return false;
    }
}

function hasDockerCGroup() {
    try {
        return fs.readFileSync('/proc/self/cgroup', 'utf8').includes('docker');
    } catch {
        return false;
    }
}

export default function isDocker() {
    // TODO: Use `??=` when targeting Node.js 16.
    if (isDockerCached === undefined) {
        isDockerCached = hasDockerEnv() || hasDockerCGroup();

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key