Dockerode is a comprehensive Node.js library that provides a complete interface to Docker's Remote API. It enables programmatic interaction with Docker containers, images, volumes, networks, and other Docker entities through both callback and Promise-based interfaces. The library maintains stream compatibility and includes entity-based architecture where containers, images, and exec instances are treated as first-class objects.
npm install dockerodeconst Docker = require('dockerode');For ES modules:
import Docker from 'dockerode';const Docker = require('dockerode');
// Create Docker client instance
const docker = new Docker();
// List all containers
const containers = await docker.listContainers({ all: true });
// Create and start a container
const container = await docker.createContainer({
Image: 'ubuntu:latest',
Cmd: ['/bin/bash'],
name: 'my-container'
});
await container.start();
// Get container information
const info = await container.inspect();
console.log(info.State.Status);Dockerode is built around several key components:
Main Docker client providing factory methods, listing operations, creation methods, and system information.
class Docker {
constructor(opts?: DockerOptions);
// Factory methods
getContainer(id: string): Container;
getImage(name: string): Image;
getVolume(name: string): Volume;
getNetwork(id: string): Network;
getService(id: string): Service;
// System information
info(opts?: object, callback?: Function): Promise<object>;
version(opts?: object, callback?: Function): Promise<object>;
ping(opts?: object, callback?: Function): Promise<string>;
}
interface DockerOptions {
socketPath?: string;
host?: string;
port?: number;
ca?: string;
cert?: string;
key?: string;
protocol?: string;
timeout?: number;
version?: string;
Promise?: any;
}Complete container lifecycle management including creation, execution, monitoring, and file operations.
class Container {
id: string;
inspect(opts?: object, callback?: Function): Promise<object>;
start(opts?: object, callback?: Function): Promise<object>;
stop(opts?: object, callback?: Function): Promise<object>;
remove(opts?: object, callback?: Function): Promise<object>;
logs(opts?: object, callback?: Function): Promise<Stream>;
exec(opts: ExecOptions, callback?: Function): Promise<Exec>;
}
interface ExecOptions {
AttachStdout?: boolean;
AttachStderr?: boolean;
AttachStdin?: boolean;
DetachKeys?: string;
Tty?: boolean;
Cmd: string[];
Env?: string[];
Privileged?: boolean;
User?: string;
WorkingDir?: string;
}Image operations including pulling, building, pushing, and inspection.
class Image {
id: string;
inspect(opts?: object, callback?: Function): Promise<object>;
history(callback?: Function): Promise<object[]>;
push(opts?: object, callback?: Function, auth?: object): Promise<Stream>;
tag(opts: TagOptions, callback?: Function): Promise<object>;
remove(opts?: object, callback?: Function): Promise<object[]>;
}
interface TagOptions {
repo: string;
tag?: string;
force?: boolean;
}Docker volume and network management capabilities.
class Volume {
name: string;
inspect(opts?: object, callback?: Function): Promise<object>;
remove(opts?: object, callback?: Function): Promise<object>;
}
class Network {
id: string;
inspect(opts?: object, callback?: Function): Promise<object>;
remove(opts?: object, callback?: Function): Promise<object>;
connect(opts: ConnectOptions, callback?: Function): Promise<object>;
disconnect(opts: DisconnectOptions, callback?: Function): Promise<object>;
}Complete Docker Swarm management including services, nodes, secrets, and configs.
class Service {
id: string;
inspect(opts?: object, callback?: Function): Promise<object>;
update(auth?: object, opts?: object, callback?: Function): Promise<object>;
remove(opts?: object, callback?: Function): Promise<object>;
logs(opts?: object, callback?: Function): Promise<Stream>;
}
// Swarm management methods on Docker client
docker.swarmInit(opts: SwarmInitOptions, callback?: Function): Promise<object>;
docker.swarmJoin(opts: SwarmJoinOptions, callback?: Function): Promise<object>;
docker.swarmLeave(opts?: object, callback?: Function): Promise<object>;Docker plugin lifecycle and configuration management.
class Plugin {
name: string;
inspect(opts?: object, callback?: Function): Promise<object>;
enable(opts?: object, callback?: Function): Promise<object>;
disable(opts?: object, callback?: Function): Promise<object>;
remove(opts?: object, callback?: Function): Promise<object>;
upgrade(auth?: object, opts?: object, callback?: Function): Promise<Stream>;
}All entity classes are exposed as static properties on the Docker class for direct instantiation:
Docker.Container = Container;
Docker.Image = Image;
Docker.Volume = Volume;
Docker.Network = Network;
Docker.Service = Service;
Docker.Plugin = Plugin;
Docker.Secret = Secret;
Docker.Config = Config;
Docker.Task = Task;
Docker.Node = Node;
Docker.Exec = Exec;All methods support both callback and Promise patterns:
// Callback style
docker.listContainers((err, containers) => {
if (err) throw err;
console.log(containers);
});
// Promise style
docker.listContainers()
.then(containers => console.log(containers))
.catch(err => console.error(err));
// Async/await style
const containers = await docker.listContainers();Many methods support AbortSignal for request cancellation:
const controller = new AbortController();
docker.listContainers({ abortSignal: controller.signal });
// Cancel the request
controller.abort();Methods returning large amounts of data support streaming:
// Container logs as stream
const logStream = await container.logs({
stdout: true,
stderr: true,
follow: true
});
logStream.on('data', chunk => {
console.log(chunk.toString());
});