Skip to content

theogravity/node-docker-id

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

node-docker-id

npm version CircleCI

Gets the docker container id / checks if the app is running in a docker container via CLI or API.

Install

yarn add docker-id

API Usage

Two methods are available:

  • getContainerId (async version - uses fs.readFile; does not throw anything at all in fail cases, just returns null)
  • getContainerIdSync (sync version - uses fs.readFileSync)

The result is cached; subsequent calls will not result in additional file reads.

You can wrap the result with Boolean() to turn it into a true / false value. Useful if you want to just check that the app is in a container or not.

  • Boolean(getContainerIdSync())
  • Boolean(await getContainerId())

Returns:

  • null if the id cannot be found or the app is not running in docker
  • container id if it is found
import {
  getContainerId,
  getContainerIdSync
} from 'docker-id'

// promisified / async version
getContainerId.then((id) => {
  console.log(id)
})

// ------

// sync version
const containerId = getContainerIdSync()

// If you want to check if it is in docker or not
const isDocker = Boolean(containerId)

CLI usage

$ get-container-id

  • Outputs the id if found with an exit code of 0
  • If not found, outputs an error message with an exit code of -1

About

Gets the docker container id / checks if the app is running in a docker container via CLI or API

Resources

License

Stars

Watchers

Forks

Packages

No packages published