Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Posting minor alteration to work with nodejs require() - for anyone reading who isn't using experimental modules/.mjs in node - perhaps can make this a gist #13

Closed
n2nco opened this issue Aug 22, 2022 · 1 comment

Comments

@n2nco
Copy link

n2nco commented Aug 22, 2022

let fs = require('fs')

let isDockerCached;

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;
	}
}

module.exports = function isDocker() {
        if (isDockerCached === undefined) {
            isDockerCached = hasDockerEnv() || hasDockerCGroup();
        }
        return isDockerCached;
    }

@sindresorhus
Copy link
Owner

@sindresorhus sindresorhus closed this as not planned Won't fix, can't repro, duplicate, stale Aug 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants