Registry / devops / dockerode

dockerode

JSON →
library4.0.10jsnpmunverified

Dockerode is a comprehensive Node.js module designed for programmatically interacting with the Docker Remote API. It provides a robust, feature-rich interface for managing Docker containers, images, networks, and other Docker entities, aiming to implement all features exposed by the Docker Remote API. Key differentiators include its strong emphasis on native Node.js streams for operations like logs and execs, allowing for flexible stream manipulation and demultiplexing. It treats Docker entities (containers, images, execs) as distinct objects, and offers both callback and Promise-based interfaces, catering to various asynchronous programming styles. The package maintains a steady release cadence with frequent patch updates for dependencies and minor bug fixes, with the current stable version being 4.0.10. It is built to be highly testable and closely track changes in the official Docker API, acting as a direct wrapper that passes options to Docker and returns its responses largely unchanged.

npm install dockerode
INSTALL
IMPORT
SIG · DOCKERODE
D
dockerode
devopsjavascriptv4.0.10
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Docker
import Docker from 'dockerode';
import { Docker } from 'dockerode';
While CommonJS usage (`const Docker = require('dockerode');`) is common in older projects, modern TypeScript and ESM environments should use a default import for the main `Docker` class, as it's the primary export.
Docker (CommonJS)
const Docker = require('dockerode');
This is the traditional and fully supported way to import Dockerode in CommonJS environments, aligning with many examples in the official documentation.
Container/Image types
import type { Container, Image, ImageInfo, ContainerInfo } from 'dockerode';
Specific TypeScript types for Docker entities (like `Container`, `Image`) and API response structures (e.g., `ImageInfo`, `ContainerInfo`) are exported directly. Consult the `index.d.ts` file for a comprehensive list of available types.

This example demonstrates a full container lifecycle: pulling an image, creating, starting, attaching to logs, waiting for exit, and removing a Docker container using the Promise-based API.

import Docker from 'dockerode'; import process from 'process'; import stream from 'stream'; const docker = new Docker({ socketPath: process.env.DOCKER_SOCKET_PATH || '/var/run/docker.sock', // Default for Linux host: process.env.DOCKER_HOST || undefined, // e.g., 'http://192.168.1.10' port: process.env.DOCKER_PORT || undefined, // e.g., 2375 version: 'v1.41' // It's recommended to specify your Docker daemon's API version }); async function runDockerLifecycleExample() { let auxContainer: Docker.Container | undefined; const imageName = 'ubuntu:latest'; try { console.log(`Checking if image '${imageName}' exists...`); const images = await docker.listImages({ filters: { reference: [imageName] } }); if (images.length === 0) { console.log(`Image '${imageName}' not found locally. Pulling...`); await docker.pull(imageName, {}); console.log(`Image '${imageName}' pulled.`); } console.log('Creating a new Ubuntu container...'); auxContainer = await docker.createContainer({ Image: imageName, AttachStdin: false, AttachStdout: true, AttachStderr: true, Tty: true, Cmd: ['/bin/bash', '-c', 'echo "Hello from Dockerode inside container!"; sleep 3; echo "Exiting."; exit 0;'], OpenStdin: false, StdinOnce: false }); console.log(`Container created with ID: ${auxContainer.id}`); console.log('Starting container...'); await auxContainer.start(); console.log('Container started. Attaching to logs...'); const logStream = await auxContainer.logs({ follow: true, stdout: true, stderr: true }); const outputStream = new stream.PassThrough(); logStream.pipe(outputStream); outputStream.on('data', (chunk) => console.log(`[LOG]: ${chunk.toString('utf8').trim()}`)); outputStream.on('end', () => console.log('Log stream ended.')); console.log('Waiting for container to exit...'); const exitResult = await auxContainer.wait(); console.log(`Container exited with status code: ${exitResult.StatusCode}`); console.log('Removing container...'); await auxContainer.remove(); console.log('Container removed successfully.'); } catch (err) { console.error('An error occurred during Docker operation:', err); if (auxContainer) { try { console.error('Attempting to force-remove container due to error...'); await auxContainer.remove({ force: true }); console.error('Container force-removed.'); } catch (removeErr) { console.error('Failed to force-remove container:', removeErr); } } process.exit(1); } } runDockerLifecycleExample();
Debug
Known issues
breakingMajor version 4.0.0 potentially introduces breaking changes due to significant dependency updates (e.g., `docker-modem`) and internal refactors. While specific API changes aren't detailed in the changelog, users upgrading from v3.x should thoroughly test their integrations. Changes might include updated types, refined method signatures, or altered default behaviors.
fix
Review your code for compatibility with the latest Docker API and dockerode's internal updates. Check the `docker-modem` changelog for more details on underlying network stack changes if issues arise.
affects: >=4.0.0
breakingVersion 4.0.2 included an important security update for the underlying `SSH2` dependency (via `docker-modem`) to address CVE-2023-48795. This is a critical security fix for users connecting to Docker daemons via SSH. Ensure you are on at least v4.0.2 or a higher patched version.
fix
Upgrade `dockerode` to version 4.0.2 or later to mitigate CVE-2023-48795.
affects: <4.0.2
gotchaIncorrect Docker API versioning can lead to unexpected errors or unsupported operations. Dockerode explicitly recommends specifying the API version for Docker versions >= v1.13. Failing to set the 'version' option or setting an incompatible version will result in API errors directly from the Docker daemon.
fix
Always explicitly set the `version` option in the `Docker` constructor, e.g., `'version: 'v1.41''. Match this version to your Docker daemon's API version for optimal compatibility. You can retrieve it via `docker version --format '{{.Server.APIVersion}}'`.
affects: all
gotchaDockerode leverages Node.js streams extensively for operations like container logs, exec streams, and build streams. Improperly handling or not consuming these streams can lead to resource leaks, process hangs, or unexpected behavior due to unread data or unclosed connections. Always ensure streams are properly piped, consumed, or closed.
fix
Implement robust stream handling, including error listeners and 'end' event listeners. Use utility functions like `demuxStream` when dealing with multiplexed Docker streams. Ensure backpressure is managed if processing large amounts of data to prevent memory issues.
affects: all
Errors
Common errors & fixes
connect ECONNREFUSED [host]:[port]
The Docker daemon is not running, or `dockerode` is configured to connect to an incorrect host, port, or socket path.
fix
Verify your Docker daemon is running (`sudo systemctl start docker` or `docker start`). Check your Dockerode connection configuration (e.g., `socketPath`, `host`, `port`, or the `DOCKER_HOST` environment variable) and ensure it matches your Docker setup.
(HTTP code 404) no such container - No such container: [container_id_or_name]
Attempting to interact with a Docker container that does not exist or whose ID/name is incorrect for the connected Docker daemon.
fix
Double-check the container ID or name you are using. Ensure the container exists on the Docker daemon you are connected to by listing all containers (`docker ps -a`).
(HTTP code 400) bad parameter - API version is too old. Minimum is vX.Y, got vA.B
The Docker API version specified in dockerode's configuration (`version` option) is incompatible with the Docker daemon's API version.
fix
Update the `version` option in your `new Docker()` constructor to match your Docker daemon's API version (e.g., 'v1.41'). You can find your daemon's API version with `docker version --format '{{.Server.APIVersion}}'`.
Cannot read properties of undefined (reading 'start')
An operation on a Docker entity (like `container.start()`) is being called on an `undefined` object. This typically happens when a preceding asynchronous operation (e.g., `docker.createContainer()`) failed or returned `null`/`undefined` without proper error handling.
fix
Ensure all asynchronous Dockerode operations are `await`ed or chained with `.then().catch()` appropriately. Always check that the resulting entity objects are valid before attempting subsequent operations on them to prevent `undefined` references.
Upgrade
Version history
4.0.10latest on npm
Audit
Dependencies
docker-modemrequiredCore networking and API communication layer for Dockerode.
Agent activity
12 hits · last 30 days
node
11
OpenAI (training)
1
Resources