Docker Machine CLI Wrapper
dockermachine-cli-js is a Node.js wrapper library that provides a programmatic interface for interacting with the `docker-machine` command-line tool. It abstracts the execution of `docker-machine` commands, offering both Promise-based and callback-style APIs for convenience. The current stable version is 3.0.5. While releases appear infrequent, the project has seen recent updates in the 3.x series, indicating active maintenance. A key differentiator is its ability to parse the output of commands like `ls` into structured JavaScript objects, rather than just returning raw string output. It also ships with TypeScript type definitions, enabling a robust development experience for TypeScript users. This library is specifically useful for automating `docker-machine` provisioning and management tasks within Node.js applications, offering a more structured approach than direct `child_process` execution. A crucial prerequisite is the external installation and availability of the `docker-machine` CLI tool on the system's PATH, as this library acts as a thin wrapper around it.
Common errors
-
Error: spawn docker-machine ENOENT
cause The `docker-machine` command-line tool is not installed or is not accessible in the system's PATH environment variable.fixInstall `docker-machine` on your system according to the official Docker documentation. After installation, verify it's available by running `docker-machine version` in your terminal. If installed but not found, ensure its executable directory is added to your system's PATH. -
TypeError: DockerMachineCLI.Options is not a constructor
cause This error typically occurs when attempting to instantiate `Options` directly from `DockerMachineCLI` in an ESM context where named imports are expected, or if an older CommonJS pattern is mixed with newer ESM imports.fixFor TypeScript/ESM, use named imports: `import { DockerMachine, Options } from 'dockermachine-cli-js';` then `new Options(...)`. For CommonJS, ensure `const DockerMachineCLI = require('dockermachine-cli-js');` is used and then `new DockerMachineCLI.Options(...)`. -
UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: X): Error: ...
cause The Promise returned by `dockerMachine.command()` was not handled with a `.catch()` block, leading to an unhandled rejection if the underlying `docker-machine` command fails.fixAlways append a `.catch(error => { console.error('Command failed:', error); })` to your `dockerMachine.command()` calls to properly handle errors and prevent unhandled promise rejections.
Warnings
- gotcha The `docker-machine` command-line tool must be installed separately on the system where this Node.js package is run. This package is a wrapper, not an installer or bundler, of the `docker-machine` CLI. Ensure it's in your system's PATH.
- breaking In version 3.0.2, the constructor for `Options` was changed to directly accept a `keyValueObject` as its first argument. Previous versions might have used a different structure or relied on implicit argument handling.
- breaking Version 2.0 introduced TypeScript support. While this generally improves type safety, it might have involved changes to the module's internal structure or export patterns. Users migrating from pre-2.0 versions might need to update their import statements or how they access `DockerMachineCLI` components, especially in TypeScript projects.
Install
-
npm install dockermachine-cli-js -
yarn add dockermachine-cli-js -
pnpm add dockermachine-cli-js
Imports
- DockerMachine, Options
import { DockerMachine, Options } from 'dockermachine-cli-js'; - DockerMachineCLI
import DockerMachineCLI from 'dockermachine-cli-js';
const DockerMachineCLI = require('dockermachine-cli-js'); - Options
const options = new Options(...);
const options = new DockerMachineCLI.Options(...);
Quickstart
import { DockerMachine, Options } from 'dockermachine-cli-js';
// Placeholder for configuration, replace with actual values or environment variables
const config = {
accessKeyId: process.env.AWS_ACCESS_KEY_ID ?? 'YOUR_AWS_ACCESS_KEY_ID',
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY ?? 'YOUR_AWS_SECRET_ACCESS_KEY'
};
const keyValueObject = {
'driver': 'amazonec2',
'amazonec2-access-key': config.accessKeyId,
'amazonec2-secret-key': config.secretAccessKey,
'amazonec2-ami': 'ami-b59ce48f', // Example AMI, choose appropriate for your region
'amazonec2-region': 'ap-southeast-2',
'amazonec2-zone': 'a',
'amazonec2-instance-type': 't2.micro',
'amazonec2-root-size': 8
};
const options = new Options(
/* keyValueObject */ keyValueObject,
/* currentWorkingDirectory */ null
);
const dockerMachine = new DockerMachine(options);
console.log('Attempting to create a Docker Machine...');
dockerMachine.command('create my-test-machine')
.then(function (data) {
console.log('Machine creation command output:', data);
return dockerMachine.command('ls');
})
.then(function (data) {
console.log('List of Docker Machines:', data.machineList);
// Clean up: Delete the created machine
console.log('Attempting to remove the Docker Machine...');
return dockerMachine.command('rm -f my-test-machine');
})
.then(function (data) {
console.log('Machine removal command output:', data);
console.log('Cleanup complete.');
})
.catch(function (err) {
console.error('An error occurred:', err);
});