docker-api-ts v0.2.2
Docker API TypeScript
docker-api-ts is an open-source TypeScript package that provides a comprehensive client for interacting with the Docker Engine API. This package includes various services to manage Docker containers, images, networks, volumes, and more.
Features
- Container Management: Create, inspect, start, stop, and remove containers.
- Image Management: Pull, tag, and remove Docker images.
- Service Management: Create, update, and remove services in Docker Swarm mode.
- System Operations: Get Docker system information and manage system-wide settings.
- Execution Management: Run commands inside running containers.
- Volume Management: Create, inspect, and remove Docker volumes.
- Network Management: Create, inspect, and remove Docker networks.
- Plugin Management: Install, enable, disable, and remove Docker plugins.
- Node Management: Manage nodes in Docker Swarm.
- Swarm Management: Initialize, join, and leave Docker Swarm.
- Task Management: Inspect tasks in Docker Swarm.
- Secret Management: Create, inspect, and remove Docker secrets.
- Config Management: Create, inspect, and remove Docker configs.
Installation
Install the package using npm for Node.js users:
npm install docker-api-ts
# OR JSR
npx jsr add @rainypixel/docker-api-tsFor Deno users, you can import the module directly from the URL:
# ONLY JSR allowed
deno add @rainypixel/docker-api-tsimport Docker from '@rainypixel/docker-api-ts';Usage
Here is an example of how to use the Docker API TypeScript client:
// Node.JS
import Docker from 'docker-api-ts';
// Deno
import Docker from '@rainypixel/docker-api-ts';
// Initialize the Docker client
const docker = new Docker('/var/run/docker.sock', null);
// List all containers
const containers = await docker.containers.containerList();
console.log(containers.body)
// Pull an image
const data = await docker.images.imageCreate({ fromImage: 'alpine', tag: 'latest' });
console.log(data.status);
// Create a new container
const data = await docker.containers.containerCreate({
body: {
Image: 'alpine:latest',
Cmd: ['echo', 'hello world'],
},
});
if (data.body?.Id) {
await docker.containers.containerStart({ id: data.body.Id });
}Connecting via TCP
You can also connect to the Docker daemon via TCP. Note that TCP TLS support is not ready yet and will be available in the next version.
Example
// Node.JS
import Docker from 'docker-api-ts';
// Deno
import Docker from '@rainypixel/docker-api-ts';
// Initialize the Docker client with TCP
const docker = new Docker({ host: '127.0.0.1', port: 2375, transport: 'tcp' }, null);
// List all containers
const containers = await docker.containers.containerList();
console.log(containers.body)API Reference
Docker
The main class to interact with Docker services.
Constructor
new Docker(connectSetting: string | Deno.ConnectOptions, auth: RegistryAuthentication | null = null)- connectSetting: Connection settings as a string (if unix), or object if TCP (e.g.,
/var/run/docker.sockor{ host: '127.0.0.1', port: 2375, transport: 'tcp' }). - auth: Optional authentication settings for Docker registry.
Services
- containers: Instance of
ContainerServiceto manage Docker containers. - images: Instance of
ImageServiceto manage Docker images. - services: Instance of
ServiceServiceto manage Docker services. - system: Instance of
SystemServiceto manage Docker system operations. - exec: Instance of
ExecServiceto run commands in containers. - volume: Instance of
VolumeServiceto manage Docker volumes. - network: Instance of
NetworkServiceto manage Docker networks. - plugin: Instance of
PluginServiceto manage Docker plugins. - node: Instance of
NodeServiceto manage Docker Swarm nodes. - swarm: Instance of
SwarmServiceto manage Docker Swarm operations. - task: Instance of
TaskServiceto inspect Docker Swarm tasks. - secret: Instance of
SecretServiceto manage Docker secrets. - config: Instance of
ConfigServiceto manage Docker configs.
Contributing
We welcome contributions! Please see the CONTRIBUTING.md for details on how to get started.
License
This project is licensed under the MIT License. See the LICENSE file for details.