@procedures/docker

Docker container management as procedures using client-shell
Overview
client-docker wraps Docker CLI commands as procedures using client-shell. It provides a comprehensive set of procedures for container management, image building, Docker Compose orchestration, and more - all accessible through the procedure system's RPC interface.
Key Features
- Comprehensive Docker Coverage: Run, build, pull, push, tag, exec, stop, rm, ps, logs, images, inspect, volume, network and prune commands
- Docker Compose Support: Up and down operations for multi-container applications
- Type-Safe Inputs: Zod schemas for all inputs with full TypeScript support
- Shell Integration: Leverages
client-shell for reliable CLI execution
- Consistent Output: Standardized output format across all procedures
- CLI Wrapper Pattern: One procedure per Docker command with full option support
Architecture
graph TB
subgraph "client-docker"
REG[Registration Layer]
PROC[Docker Procedures]
TYPES[Type System]
end
subgraph "Procedures"
RUN[docker.run]
BUILD[docker.build]
PULL[docker.pull]
EXEC[docker.exec]
STOP[docker.stop]
RM[docker.rm]
PS[docker.ps]
LOGS[docker.logs]
UP[docker.compose.up]
DOWN[docker.compose.down]
end
subgraph "client-shell"
SHELL[shell.exec]
end
REG --> PROC
PROC --> RUN
PROC --> BUILD
PROC --> PULL
PROC --> EXEC
PROC --> STOP
PROC --> RM
PROC --> PS
PROC --> LOGS
PROC --> UP
PROC --> DOWN
RUN --> SHELL
BUILD --> SHELL
PULL --> SHELL
EXEC --> SHELL
STOP --> SHELL
RM --> SHELL
PS --> SHELL
LOGS --> SHELL
UP --> SHELL
DOWN --> SHELL
style REG fill:#4a90e2
style PROC fill:#7ed321
style SHELL fill:#f5a623
Data Flow
sequenceDiagram
participant Client
participant docker.run
participant shell.exec
participant Docker CLI
Client->>docker.run: { image, ports, env, ... }
docker.run->>docker.run: Build docker run command
docker.run->>shell.exec: { command: "docker run ..." }
shell.exec->>Docker CLI: Execute command
Docker CLI-->>shell.exec: stdout/stderr/exitCode
shell.exec-->>docker.run: ShellResult
docker.run->>docker.run: Format as DockerCommandOutput
docker.run-->>Client: { exitCode, stdout, stderr, success, duration }
Installation
npm install @procedures/docker
Or with pnpm:
pnpm add @procedures/docker
Dependencies
This package depends on:
@procedures/client - Core client/procedure system
@procedures/shell - Shell execution for CLI commands
zod - Schema validation and type inference
System Requirements
- Docker must be installed and available in PATH
- For Docker Compose procedures: Docker Compose must be installed
Usage
Basic Container Operations
import { Client, LocalTransport, PROCEDURE_REGISTRY } from "@procedures/client";
import "@procedures/docker/register"; // registers its procedures
const client = new Client(new LocalTransport({ registry: PROCEDURE_REGISTRY }));
// Run a container
const result = await client.exec(["docker", "run"], {
image: "nginx:latest",
name: "my-nginx",
ports: ["8080:80"],
detach: true,
});
console.log(result);
// {
// exitCode: 0,
// stdout: "container_id_here\n",
// stderr: "",
// success: true,
// duration: 1234
// }
Building Images
// Build an image from a Dockerfile
await client.exec(["docker", "build"], {
context: ".",
tag: "my-app:latest",
dockerfile: "Dockerfile",
buildArgs: {
NODE_VERSION: "20",
APP_ENV: "production",
},
noCache: false,
});
Executing Commands in Containers
// Execute a command in a running container
await client.exec(["docker", "exec"], {
container: "my-nginx",
command: "nginx -t",
interactive: false,
tty: false,
});
// Interactive shell session
await client.exec(["docker", "exec"], {
container: "my-app",
command: "/bin/bash",
interactive: true,
tty: true,
env: {
DEBUG: "true",
},
});
Container Management
// List all containers
const containers = await client.exec(["docker", "ps"], {
all: true,
format: "json",
});
// Stop containers
await client.exec(["docker", "stop"], {
containers: ["nginx-1", "nginx-2"],
time: 10, // Grace period in seconds
});
// Remove containers
await client.exec(["docker", "rm"], {
containers: ["nginx-1"],
force: true,
volumes: true,
});
// Get container logs
const logs = await client.exec(["docker", "logs"], {
container: "my-app",
tail: 100,
timestamps: true,
follow: false,
});
Docker Compose
// Start services
await client.exec(["docker", "compose", "up"], {
file: "docker-compose.yml",
detach: true,
build: true,
forceRecreate: false,
services: ["web", "db"], // Optional: specific services
});
// Stop and remove services
await client.exec(["docker", "compose", "down"], {
file: "docker-compose.yml",
volumes: true,
removeOrphans: true,
rmi: "local", // Remove images built locally
});
API Reference
Common Output Type
All Docker procedures return a standardized output format:
interface DockerCommandOutput {
exitCode: number;
stdout: string;
stderr: string;
success: boolean; // true if exitCode === 0
duration: number; // milliseconds
}
Procedures
docker.run
Run a Docker container.
Input:
{
image: string; // Required: Docker image
name?: string; // Container name
ports?: string[]; // Port mappings ["host:container", ...]
volumes?: string[]; // Volume mappings ["/host:/container", ...]
env?: Record<string, string>; // Environment variables
detach?: boolean; // Run in background (default: false)
rm?: boolean; // Auto-remove when stopped (default: false)
command?: string; // Command to run in container, split at whitespace
args?: string[]; // The command as a list: each item is one argument (not with command)
cwd?: string; // Working directory for command
timeout?: number; // Command timeout in ms
}
Example:
await client.exec(["docker", "run"], {
image: "postgres:15",
name: "my-db",
ports: ["5432:5432"],
env: {
POSTGRES_PASSWORD: "secret",
POSTGRES_DB: "myapp",
},
detach: true,
});
docker.build
Build a Docker image from a Dockerfile.
Input:
{
context: string; // Required: Build context path
tag: string; // Required: Image tag
dockerfile?: string; // Dockerfile path (relative to context)
buildArgs?: Record<string, string>; // Build arguments
noCache?: boolean; // Don't use cache (default: false)
cwd?: string; // Working directory
timeout?: number; // Build timeout in ms
}
Example:
await client.exec(["docker", "build"], {
context: ".",
tag: "my-app:v1.0.0",
buildArgs: {
NODE_VERSION: "20",
},
noCache: true,
});
docker.pull
Pull a Docker image from a registry.
Input:
{
image: string; // Required: Image name
tag?: string; // Image tag (default: "latest")
cwd?: string; // Working directory
timeout?: number; // Pull timeout in ms
}
Example:
await client.exec(["docker", "pull"], {
image: "nginx",
tag: "alpine",
});
docker.exec
Execute a command in a running container.
Input:
{
container: string; // Required: Container name or ID
command?: string; // Command to execute, split at whitespace
args?: string[]; // The command as a list: each item is one argument. Give command or args
interactive?: boolean; // Allocate stdin (default: false)
tty?: boolean; // Allocate pseudo-TTY (default: false)
workdir?: string; // Working directory inside container
env?: Record<string, string>; // Environment variables
cwd?: string; // Working directory (host)
timeout?: number; // Command timeout in ms
}
Example:
await client.exec(["docker", "exec"], {
container: "my-app",
command: "npm run migrate",
env: {
DATABASE_URL: "postgres://...",
},
});
docker.stop
Stop running containers.
Input:
{
containers: string[]; // Required: Container names/IDs
time?: number; // Seconds to wait before killing (default: 10)
cwd?: string; // Working directory
timeout?: number; // Command timeout in ms
}
Example:
await client.exec(["docker", "stop"], {
containers: ["web-1", "web-2", "worker-1"],
time: 30,
});
docker.rm
Remove containers.
Input:
{
containers: string[]; // Required: Container names/IDs
force?: boolean; // Force removal (default: false)
volumes?: boolean; // Remove volumes (default: false)
cwd?: string; // Working directory
timeout?: number; // Command timeout in ms
}
Example:
await client.exec(["docker", "rm"], {
containers: ["temp-container"],
force: true,
volumes: true,
});
docker.ps
List containers.
Input:
{
all?: boolean; // Show all containers (default: false)
filter?: string; // Filter output (e.g., "status=running")
format?: string; // Format output (e.g., "json")
cwd?: string; // Working directory
timeout?: number; // Command timeout in ms
}
Example:
const result = await client.exec(["docker", "ps"], {
all: true,
format: "json",
});
const containers = JSON.parse(result.stdout);
docker.logs
Get container logs.
Input:
{
container: string; // Required: Container name or ID
follow?: boolean; // Follow log output (default: false)
tail?: number; // Number of lines from end
timestamps?: boolean; // Show timestamps (default: false)
cwd?: string; // Working directory
timeout?: number; // Command timeout in ms
}
Example:
const logs = await client.exec(["docker", "logs"], {
container: "my-app",
tail: 50,
timestamps: true,
});
console.log(logs.stdout);
docker.compose.up
Start services with Docker Compose.
Input:
{
file?: string; // Compose file path (default: "docker-compose.yml")
services?: string[]; // Services to start (default: all)
detach?: boolean; // Run in background (default: false)
build?: boolean; // Build images before starting (default: false)
forceRecreate?: boolean; // Force recreate containers (default: false)
cwd?: string; // Working directory
timeout?: number; // Command timeout in ms
}
Example:
await client.exec(["docker", "compose", "up"], {
file: "docker-compose.prod.yml",
services: ["web", "db"],
detach: true,
build: true,
});
docker.compose.down
Stop and remove services with Docker Compose.
Input:
{
file?: string; // Compose file path (default: "docker-compose.yml")
volumes?: boolean; // Remove volumes (default: false)
removeOrphans?: boolean; // Remove orphan containers (default: false)
rmi?: "all" | "local"; // Remove images (all or local only)
cwd?: string; // Working directory
timeout?: number; // Command timeout in ms
}
Example:
await client.exec(["docker", "compose", "down"], {
volumes: true,
removeOrphans: true,
rmi: "local",
});
More procedures (roadmap R41)
Each procedure gives the common output type, and takes cwd and timeout. A failed command does not throw. A name, an image or a service that starts with - is refused, because docker reads it as an option. The effects column shows metadata.effects.
| Procedure |
Input |
Effects |
docker.compose.ps |
file, services, all, format |
reads docker and fs |
docker.compose.logs |
file, services, tail, since, timestamps |
reads docker and fs |
docker.compose.build |
file, services, noCache, pull, buildArgs |
reads docker, fs and network, writes docker and process, not destructive |
docker.compose.restart |
file, services, time |
reads docker and fs, writes docker |
docker.images |
repository, all, filter, format |
reads docker |
docker.inspect |
targets (one or more), type, format |
reads docker |
docker.volume.list |
filter, format |
reads docker |
docker.volume.create |
name, driver, labels |
writes docker, not destructive, idempotent |
docker.volume.remove |
volumes (one or more), force |
writes docker, destructive, idempotent |
docker.network.list |
filter, format |
reads docker |
docker.network.create |
name, driver, internal, labels |
writes docker, not destructive, idempotent |
docker.network.remove |
networks (one or more) |
writes docker, destructive, idempotent |
docker.tag |
source, target |
writes docker, idempotent |
docker.push |
image, allTags |
reads docker and network, writes network, idempotent |
docker.prune |
kind, all, volumes, filter, confirm |
writes docker, destructive, idempotent |
docker.inspect gives data: the parsed JSON of docker. With format, docker writes text, and data is absent.
docker.compose.logs does not follow the logs. It gives the lines that exist.
docker.prune needs confirm: true. The removed objects and their data are lost. The kind is system (the default), container, image, volume, network or builder.
docker.tag moves the name from another image. docker.push replaces the image of the tag in the registry.
docker.run and docker.exec take args, a list of arguments, in place of command. Docker gets each item as one argument, so an argument can contain spaces and quotes. command still works, split at whitespace. The two together are an error.