├── .editorconfig ├── .gitattributes ├── .github ├── funding.yml ├── security.md └── workflows │ └── main.yml ├── .gitignore ├── .npmrc ├── cli.js ├── index.d.ts ├── index.js ├── index.test-d.ts ├── license ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.github/funding.yml: -------------------------------------------------------------------------------- 1 | github: sindresorhus 2 | open_collective: sindresorhus 3 | tidelift: npm/is-docker 4 | custom: https://sindresorhus.com/donate 5 | -------------------------------------------------------------------------------- /.github/security.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. 4 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 16 14 | steps: 15 | - uses: actions/checkout@v2 16 | - uses: actions/setup-node@v2 17 | with: 18 | node-version: ${{ matrix.node-version }} 19 | - run: npm install 20 | - run: npm test 21 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /cli.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | import process from 'node:process'; 3 | import isDocker from './index.js'; 4 | 5 | process.exitCode = isDocker() ? 0 : 2; 6 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | Check if the process is running inside a Docker container. 3 | 4 | @example 5 | ``` 6 | import isDocker from 'is-docker'; 7 | 8 | if (isDocker()) { 9 | console.log('Running inside a Docker container'); 10 | } 11 | ``` 12 | */ 13 | export default function isDocker(): boolean; 14 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import fs from 'node:fs'; 2 | 3 | let isDockerCached; 4 | 5 | function hasDockerEnv() { 6 | try { 7 | fs.statSync('/.dockerenv'); 8 | return true; 9 | } catch { 10 | return false; 11 | } 12 | } 13 | 14 | function hasDockerCGroup() { 15 | try { 16 | return fs.readFileSync('/proc/self/cgroup', 'utf8').includes('docker'); 17 | } catch { 18 | return false; 19 | } 20 | } 21 | 22 | export default function isDocker() { 23 | // TODO: Use `??=` when targeting Node.js 16. 24 | if (isDockerCached === undefined) { 25 | isDockerCached = hasDockerEnv() || hasDockerCGroup(); 26 | } 27 | 28 | return isDockerCached; 29 | } 30 | -------------------------------------------------------------------------------- /index.test-d.ts: -------------------------------------------------------------------------------- 1 | import {expectType} from 'tsd'; 2 | import isDocker from './index.js'; 3 | 4 | expectType(isDocker()); 5 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "is-docker", 3 | "version": "3.0.0", 4 | "description": "Check if the process is running inside a Docker container", 5 | "license": "MIT", 6 | "repository": "sindresorhus/is-docker", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": "./index.js", 15 | "bin": "./cli.js", 16 | "engines": { 17 | "node": "^12.20.0 || ^14.13.1 || >=16.0.0" 18 | }, 19 | "scripts": { 20 | "test": "xo && ava && tsd" 21 | }, 22 | "files": [ 23 | "index.js", 24 | "index.d.ts", 25 | "cli.js" 26 | ], 27 | "keywords": [ 28 | "detect", 29 | "docker", 30 | "dockerized", 31 | "container", 32 | "inside", 33 | "is", 34 | "env", 35 | "environment", 36 | "process" 37 | ], 38 | "devDependencies": { 39 | "ava": "^3.15.0", 40 | "sinon": "^11.1.2", 41 | "tsd": "^0.17.0", 42 | "xo": "^0.44.0" 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # is-docker 2 | 3 | > Check if the process is running inside a Docker container 4 | 5 | ## Install 6 | 7 | ```sh 8 | npm install is-docker 9 | ``` 10 | 11 | ## Usage 12 | 13 | ```js 14 | import isDocker from 'is-docker'; 15 | 16 | if (isDocker()) { 17 | console.log('Running inside a Docker container'); 18 | } 19 | ``` 20 | 21 | ## CLI 22 | 23 | ```sh 24 | is-docker 25 | ``` 26 | 27 | Exits with code 0 if inside a Docker container and 2 if not. 28 | 29 | ## Related 30 | 31 | - [is-inside-container](https://github.com/sindresorhus/is-inside-container) - Check if the process is running inside a container 32 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | /* eslint-disable unicorn/prefer-module, ava/no-skip-test */ 2 | import fs from 'node:fs'; 3 | import path from 'node:path'; 4 | import test from 'ava'; 5 | import sinon from 'sinon'; 6 | 7 | const require = {}; 8 | 9 | // TODO: Enable tests again when ESM supports loader hooks. 10 | 11 | test.skip('inside a Docker container (.dockerenv test)', t => { 12 | delete require.cache[path.join(__dirname, 'index.js')]; 13 | const isDocker = require('.'); 14 | 15 | fs.statSync = sinon.stub(fs, 'statSync'); 16 | fs.statSync.withArgs('/.dockerenv').returns({}); 17 | t.true(isDocker()); 18 | fs.statSync.restore(); 19 | }); 20 | 21 | test.skip('inside a Docker container (cgroup test)', t => { 22 | delete require.cache[path.join(__dirname, 'index.js')]; 23 | const isDocker = require('.'); 24 | 25 | fs.statSync = sinon.stub(fs, 'statSync'); 26 | fs.statSync.withArgs('/.dockerenv').throws('ENOENT, no such file or directory \'/.dockerinit\''); 27 | 28 | fs.readFileSync = sinon.stub(fs, 'readFileSync'); 29 | fs.readFileSync.withArgs('/proc/self/cgroup', 'utf8').returns('xxx docker yyyy'); 30 | 31 | t.true(isDocker()); 32 | 33 | fs.readFileSync.restore(); 34 | fs.statSync.restore(); 35 | }); 36 | 37 | test.skip('not inside a Docker container', t => { 38 | delete require.cache[path.join(__dirname, 'index.js')]; 39 | const isDocker = require('.'); 40 | 41 | fs.statSync = sinon.stub(fs, 'statSync'); 42 | fs.statSync.withArgs('/.dockerenv').throws('ENOENT, no such file or directory \'/.dockerinit\''); 43 | 44 | t.false(isDocker()); 45 | 46 | fs.statSync.restore(); 47 | }); 48 | --------------------------------------------------------------------------------