What is @types/dockerode?
@types/dockerode provides TypeScript type definitions for the dockerode library, which is a Node.js module for managing Docker containers and images. It allows developers to interact with Docker's API in a type-safe manner, making it easier to write and maintain code that manages Docker resources.
What are @types/dockerode's main functionalities?
Managing Docker Containers
This feature allows you to create and start Docker containers programmatically. The code sample demonstrates how to create a container from the 'ubuntu' image and start it.
const Docker = require('dockerode');
const docker = new Docker();
docker.createContainer({ Image: 'ubuntu', Cmd: ['/bin/bash'] }, function (err, container) {
container.start(function (err, data) {
console.log('Container started');
});
});
Managing Docker Images
This feature allows you to pull Docker images from a registry. The code sample demonstrates how to pull the 'ubuntu' image and track the progress of the download.
const Docker = require('dockerode');
const docker = new Docker();
docker.pull('ubuntu', function (err, stream) {
docker.modem.followProgress(stream, onFinished, onProgress);
function onFinished(err, output) {
console.log('Image pulled');
}
function onProgress(event) {
console.log(event);
}
});
Inspecting Docker Containers
This feature allows you to inspect the details of a Docker container. The code sample demonstrates how to retrieve and log the details of a container with a specific ID.
const Docker = require('dockerode');
const docker = new Docker();
docker.getContainer('container_id').inspect(function (err, data) {
console.log(data);
});
Listing Docker Containers
This feature allows you to list all Docker containers. The code sample demonstrates how to retrieve and log a list of all containers, including stopped ones.
const Docker = require('dockerode');
const docker = new Docker();
docker.listContainers({ all: true }, function (err, containers) {
console.log(containers);
});
Other packages similar to @types/dockerode
docker-cli-js
docker-cli-js is a Node.js module that provides a simple interface to interact with Docker using the command line interface (CLI). Unlike dockerode, which interacts directly with the Docker API, docker-cli-js executes Docker CLI commands and parses the output. This can be useful for scenarios where you prefer or need to use the Docker CLI.
node-docker-api
node-docker-api is another Node.js module for interacting with Docker's API. It provides a promise-based interface, which can be more convenient for handling asynchronous operations compared to dockerode's callback-based approach. It also offers a more modern and streamlined API design.
dockerode-promise
dockerode-promise is a wrapper around dockerode that adds promise support. This package allows you to use promises instead of callbacks when working with dockerode, making it easier to write and manage asynchronous code. It is particularly useful for developers who prefer using promises or async/await syntax.