Docker Remote API for Node.js

1.1.22 · active · verified Sun Apr 19

node-docker-api is a Node.js driver for the Docker Remote API, offering a promisified interface for interacting with the Docker daemon's containers, images, networks, and other resources. It distinguishes itself from alternatives like dockerode by providing a promise-based API and a different syntax, while internally relying on the same robust modem for communication. The library fully supports essential Docker functionalities including stream handling (e.g., for logs and stats), stream demultiplexing, entity management, and offers full ES6 support. It aims to cover the complete Docker Engine API reference, including experimental features, and provides TypeScript type definitions. As of version 1.1.22, the package is officially in a 'beta state,' indicating that users should expect potential API adjustments or minor instabilities, although core functionality is generally stable. Release cadence is not explicitly defined but follows development during its beta phase.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates the full lifecycle of a Docker container: creation, start, stop, restart, and forced removal, utilizing async/await with the promisified API.

import { Docker } from 'node-docker-api';

const docker = new Docker({ socketPath: '/var/run/docker.sock' });

async function manageContainer() {
  let containerInstance;
  try {
    console.log('Creating container...');
    const container = await docker.container.create({
      Image: 'ubuntu',
      name: 'test-node-api-container',
      Cmd: ['tail', '-f', '/dev/null'] // Keep container running
    });
    containerInstance = container;
    console.log(`Container '${container.id}' created.`);

    console.log('Starting container...');
    await container.start();
    console.log(`Container '${container.id}' started.`);

    console.log('Stopping container...');
    await container.stop();
    console.log(`Container '${container.id}' stopped.`);

    console.log('Restarting container...');
    await container.restart();
    console.log(`Container '${container.id}' restarted.`);

  } catch (error) {
    console.error('An error occurred:', error);
  } finally {
    if (containerInstance) {
      console.log('Deleting container...');
      await containerInstance.delete({ force: true });
      console.log(`Container '${containerInstance.id}' deleted.`);
    }
  }
}

manageContainer();

view raw JSON →