Docker Machine CLI Wrapper

3.0.5 · active · verified Wed Apr 22

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize `DockerMachine` with AWS EC2 driver options, create a new Docker Machine, list existing machines, and then clean up by removing the created machine, using TypeScript and Promises.

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

view raw JSON →