docker-api-ts
v0.2.2
Published
A comprehensive TypeScript client for interacting with the Docker Engine API.
Downloads
11
Maintainers
Readme
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-ts
For Deno users, you can import the module directly from the URL:
# ONLY JSR allowed
deno add @rainypixel/docker-api-ts
import 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.sock
or{ host: '127.0.0.1', port: 2375, transport: 'tcp' }
). - auth: Optional authentication settings for Docker registry.
Services
- containers: Instance of
ContainerService
to manage Docker containers. - images: Instance of
ImageService
to manage Docker images. - services: Instance of
ServiceService
to manage Docker services. - system: Instance of
SystemService
to manage Docker system operations. - exec: Instance of
ExecService
to run commands in containers. - volume: Instance of
VolumeService
to manage Docker volumes. - network: Instance of
NetworkService
to manage Docker networks. - plugin: Instance of
PluginService
to manage Docker plugins. - node: Instance of
NodeService
to manage Docker Swarm nodes. - swarm: Instance of
SwarmService
to manage Docker Swarm operations. - task: Instance of
TaskService
to inspect Docker Swarm tasks. - secret: Instance of
SecretService
to manage Docker secrets. - config: Instance of
ConfigService
to 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.
Incoming features
- Add unit tests for each service in the package. (Priority: [Medium])
- Write detailed examples for each service in the API reference section. (Priority: [Low])
- Add TLS support. (Priority: [High])