Git Product home page Git Product logo

is-docker's Introduction

is-docker

Check if the process is running inside a Docker container

Install

npm install is-docker

Usage

import isDocker from 'is-docker';

if (isDocker()) {
	console.log('Running inside a Docker container');
}

CLI

is-docker

Exits with code 0 if inside a Docker container and 2 if not.

Related

is-docker's People

Contributors

bendingbender avatar ds82 avatar dword-design avatar honkinggoose avatar markacola avatar sindresorhus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

is-docker's Issues

Cannot detect a Docker environment when running on BitBucket Pipelines

Looks like BitBucket Pipelines don't use the docker cgroup since November 10th, 2019 and this change broke the package.

.bitbucket-pipelines.yml:
image: circleci/node:10.14-browsers
pipelines:
  branches:
    master:
      - step:
          script:
            - npm install is-docker
            - ls -la /
            - cat /proc/self/cgroup
            - node -e "console.log(require('is-docker')())"
Build link:

https://bitbucket.org/andreybelym/testcafe-bitbucket/addon/pipelines/home#!/results/12

Build output:
npm install is-docker
1s
ls -la /
<1s
+ ls -la /
total 128
drwxr-xr-x.   1 root     root     4096 Nov 22 14:42 .
drwxr-xr-x.   1 root     root     4096 Nov 22 14:42 ..
drwxr-xr-x.   1 root     root     4096 Dec 27  2018 bin
drwxr-xr-x.   2 root     root     4096 Oct 20  2018 boot
drwxr-xr-x.   5 root     root      360 Nov 22 14:42 dev
-rwxr-xr-x.   1 circleci circleci   54 Dec 27  2018 docker-entrypoint.sh
drwxr-xr-x.   1 root     root     4096 Nov 22 14:42 etc
drwxr-xr-x.   1 root     root     4096 Dec 27  2018 home
drwxr-xr-x.   1 root     root     4096 Nov 16  2018 lib
drwxr-xr-x.   2 root     root     4096 Nov 12  2018 lib64
drwxr-xr-x.   2 root     root     4096 Nov 12  2018 media
drwxr-xr-x.   2 root     root     4096 Nov 12  2018 mnt
drwxr-xr-x.   1 root     root     4096 Nov 22 14:42 opt
dr-xr-xr-x. 250 root     root        0 Nov 22 14:42 proc
drwx------.   1 root     root     4096 Dec 12  2018 root
drwxr-xr-x.   1 root     root     4096 Nov 22 14:42 run
drwxr-xr-x.   1 root     root     4096 Dec 27  2018 sbin
drwxr-xr-x.   2 root     root     4096 Nov 12  2018 srv
dr-xr-xr-x.  13 root     root        0 Nov 22 07:18 sys
drwxrwxrwt.   1 root     root     4096 Nov 22 14:42 tmp
drwxr-xr-x.   1 root     root     4096 Nov 12  2018 usr
drwxr-xr-x.   1 root     root     4096 Nov 12  2018 var
cat /proc/self/cgroup
<1s
+ cat /proc/self/cgroup
11:cpuset:/kubepods/burstable/pod4e13dfb1-0d36-11ea-8fb9-0250678ee62f/ef5175c3927eab0f4922f7e4d79f4a04e2e53bfefd610d320afcbde939e8b5bc
10:blkio:/kubepods/burstable/pod4e13dfb1-0d36-11ea-8fb9-0250678ee62f/ef5175c3927eab0f4922f7e4d79f4a04e2e53bfefd610d320afcbde939e8b5bc
9:cpu,cpuacct:/kubepods/burstable/pod4e13dfb1-0d36-11ea-8fb9-0250678ee62f/ef5175c3927eab0f4922f7e4d79f4a04e2e53bfefd610d320afcbde939e8b5bc
8:memory:/kubepods/burstable/pod4e13dfb1-0d36-11ea-8fb9-0250678ee62f/ef5175c3927eab0f4922f7e4d79f4a04e2e53bfefd610d320afcbde939e8b5bc
7:pids:/kubepods/burstable/pod4e13dfb1-0d36-11ea-8fb9-0250678ee62f/ef5175c3927eab0f4922f7e4d79f4a04e2e53bfefd610d320afcbde939e8b5bc
6:perf_event:/kubepods/burstable/pod4e13dfb1-0d36-11ea-8fb9-0250678ee62f/ef5175c3927eab0f4922f7e4d79f4a04e2e53bfefd610d320afcbde939e8b5bc
5:devices:/kubepods/burstable/pod4e13dfb1-0d36-11ea-8fb9-0250678ee62f/ef5175c3927eab0f4922f7e4d79f4a04e2e53bfefd610d320afcbde939e8b5bc
4:hugetlb:/kubepods/burstable/pod4e13dfb1-0d36-11ea-8fb9-0250678ee62f/ef5175c3927eab0f4922f7e4d79f4a04e2e53bfefd610d320afcbde939e8b5bc
3:freezer:/kubepods/burstable/pod4e13dfb1-0d36-11ea-8fb9-0250678ee62f/ef5175c3927eab0f4922f7e4d79f4a04e2e53bfefd610d320afcbde939e8b5bc
2:net_cls,net_prio:/kubepods/burstable/pod4e13dfb1-0d36-11ea-8fb9-0250678ee62f/ef5175c3927eab0f4922f7e4d79f4a04e2e53bfefd610d320afcbde939e8b5bc
1:name=systemd:/kubepods/burstable/pod4e13dfb1-0d36-11ea-8fb9-0250678ee62f/ef5175c3927eab0f4922f7e4d79f4a04e2e53bfefd610d320afcbde939e8b5bc
node -e "console.log(require('is-docker')())"
<1s
+ node -e "console.log(require('is-docker')())"
false

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

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

Improve docker detection

This package returns true when I try to run in in my own system (not in docker). I use Ubuntu 18.04 and the package version is 2.2.0.

I suppose, positive match comes from the check
line.length > 0 && !line.endsWith('/') && !line.endsWith('init.scope')

Output of /proc/self/cgroup from the native system terminal:

$ cat /proc/self/cgroup
12:blkio:/user.slice
11:perf_event:/
10:cpuset:/
9:hugetlb:/
8:cpu,cpuacct:/user.slice
7:pids:/user.slice/user-1000.slice/[email protected]
6:freezer:/
5:rdma:/
4:memory:/user.slice
3:devices:/user.slice
2:net_cls,net_prio:/
1:name=systemd:/user.slice/user-1000.slice/[email protected]/gnome-terminal-server.service
0::/user.slice/user-1000.slice/[email protected]/gnome-terminal-server.service

If I check it inside a docker container, the ouput of file is the next:

[root@b3287ac06125 /]# cat /proc/self/cgroup
12:blkio:/docker/b3287ac0612556d92aa88d337e43c4aed30ffb78c7ee57f564f1291564fd9e54
11:perf_event:/docker/b3287ac0612556d92aa88d337e43c4aed30ffb78c7ee57f564f1291564fd9e54
10:cpuset:/docker/b3287ac0612556d92aa88d337e43c4aed30ffb78c7ee57f564f1291564fd9e54
9:hugetlb:/docker/b3287ac0612556d92aa88d337e43c4aed30ffb78c7ee57f564f1291564fd9e54
8:cpu,cpuacct:/docker/b3287ac0612556d92aa88d337e43c4aed30ffb78c7ee57f564f1291564fd9e54
7:pids:/docker/b3287ac0612556d92aa88d337e43c4aed30ffb78c7ee57f564f1291564fd9e54
6:freezer:/docker/b3287ac0612556d92aa88d337e43c4aed30ffb78c7ee57f564f1291564fd9e54
5:rdma:/
4:memory:/docker/b3287ac0612556d92aa88d337e43c4aed30ffb78c7ee57f564f1291564fd9e54
3:devices:/docker/b3287ac0612556d92aa88d337e43c4aed30ffb78c7ee57f564f1291564fd9e54
2:net_cls,net_prio:/docker/b3287ac0612556d92aa88d337e43c4aed30ffb78c7ee57f564f1291564fd9e54
1:name=systemd:/docker/b3287ac0612556d92aa88d337e43c4aed30ffb78c7ee57f564f1291564fd9e54
0::/system.slice/containerd.service

I don't know from where the part line.endsWith('init.scope') comes from, so I can't say how it should be modified to run correctly.
But now I had to go on previous version (where only "docker" in cgroups is being detected)

form of use

How can I use this module?

I can pass the name of the coteiner as a paramenter so it returns me the logs of the same ?

Half the size

...If i could write it the way I wanted

import {statSync, readFileSync} from 'node:fs'

let isDocker,
test = fn => {
  try {
    return fn(), 1
  } catch (_) {
    return 0
  }
}

export default () => !!(isDocker ??=
  test(() => statSync('/.dockerenv')) ||
  test(() => readFileSync('/proc/self/cgroup', 'utf8').includes('docker')))

Certainly don't follow any popular standardjs or xo rules, but hey... just wanted to give an example if i where to update it to ESM and at the same time try to come up with some new solution
require node 15 doe (cuz of ??=) ๐Ÿ˜ž

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.