node-docker-id
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 - usesfs.readFile
; does not throw anything at all in fail cases, just returnsnull
)getContainerIdSync
(sync version - usesfs.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
// promisified / async versiongetContainerId // ------ // sync versionconst containerId = // If you want to check if it is in docker or notconst isDocker = BooleancontainerId
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