@otterdeploy/docker 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +324 -0
- package/dist/index.d.mts +2011 -0
- package/dist/index.mjs +3013 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +80 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 OtterDeploy
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
# @otterdeploy/docker
|
|
2
|
+
|
|
3
|
+
Modern TypeScript Docker Engine SDK with Result-based error handling. A complete, type-safe alternative to `dockerode` and `docker-modem`.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
bun add @otterdeploy/docker
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { Docker } from "@otterdeploy/docker";
|
|
15
|
+
|
|
16
|
+
// Auto-detect from DOCKER_HOST, DOCKER_TLS_VERIFY, etc.
|
|
17
|
+
const docker = Docker.fromEnv();
|
|
18
|
+
|
|
19
|
+
// List containers
|
|
20
|
+
const result = await docker.containers.list({ all: true });
|
|
21
|
+
if (result.isOk()) {
|
|
22
|
+
console.log(result.value);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// Pull an image
|
|
26
|
+
const stream = await docker.pull("nginx:latest");
|
|
27
|
+
|
|
28
|
+
// Run a container
|
|
29
|
+
const run = await docker.run("alpine", ["echo", "hello"], {
|
|
30
|
+
autoRemove: true,
|
|
31
|
+
});
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Transport Configuration
|
|
35
|
+
|
|
36
|
+
Transports connect to the Docker daemon over different protocols.
|
|
37
|
+
|
|
38
|
+
### Auto-detect from environment
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
const docker = Docker.fromEnv();
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Reads `DOCKER_HOST`, `DOCKER_TLS_VERIFY`, `DOCKER_CERT_PATH`, and `DOCKER_CLIENT_TIMEOUT`.
|
|
45
|
+
|
|
46
|
+
### Unix Socket (default)
|
|
47
|
+
|
|
48
|
+
```ts
|
|
49
|
+
const docker = new Docker({
|
|
50
|
+
transport: { type: "unix", socketPath: "/var/run/docker.sock" },
|
|
51
|
+
apiVersion: "v1.53",
|
|
52
|
+
});
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### TCP / TLS
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
const docker = new Docker({
|
|
59
|
+
transport: {
|
|
60
|
+
type: "tcp",
|
|
61
|
+
host: "localhost",
|
|
62
|
+
port: 2376,
|
|
63
|
+
tls: {
|
|
64
|
+
ca: fs.readFileSync("/certs/ca.pem"),
|
|
65
|
+
cert: fs.readFileSync("/certs/cert.pem"),
|
|
66
|
+
key: fs.readFileSync("/certs/key.pem"),
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
apiVersion: "v1.53",
|
|
70
|
+
});
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### SSH
|
|
74
|
+
|
|
75
|
+
```ts
|
|
76
|
+
const docker = new Docker({
|
|
77
|
+
transport: {
|
|
78
|
+
type: "ssh",
|
|
79
|
+
host: "myhost",
|
|
80
|
+
port: 22,
|
|
81
|
+
username: "deploy",
|
|
82
|
+
privateKey: fs.readFileSync("~/.ssh/id_rsa", "utf8"),
|
|
83
|
+
},
|
|
84
|
+
apiVersion: "v1.53",
|
|
85
|
+
});
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### Windows Named Pipes
|
|
89
|
+
|
|
90
|
+
```ts
|
|
91
|
+
const docker = new Docker({
|
|
92
|
+
transport: { type: "npipe", path: "//./pipe/docker_engine" },
|
|
93
|
+
apiVersion: "v1.53",
|
|
94
|
+
});
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## API Reference
|
|
98
|
+
|
|
99
|
+
All methods return `Result<T, DockerError>` from [better-result](https://github.com/niconiahi/better-result). Use `isOk()` / `isErr()` for type-safe error handling.
|
|
100
|
+
|
|
101
|
+
### Containers
|
|
102
|
+
|
|
103
|
+
```ts
|
|
104
|
+
// List, create, inspect
|
|
105
|
+
const containers = await docker.containers.list({ all: true });
|
|
106
|
+
const container = await docker.containers.create({ Image: "nginx", name: "web" });
|
|
107
|
+
const info = await container.inspect();
|
|
108
|
+
|
|
109
|
+
// Lifecycle
|
|
110
|
+
await container.start();
|
|
111
|
+
await container.stop({ t: 10 });
|
|
112
|
+
await container.restart();
|
|
113
|
+
await container.kill({ signal: "SIGTERM" });
|
|
114
|
+
await container.pause();
|
|
115
|
+
await container.unpause();
|
|
116
|
+
await container.remove({ force: true });
|
|
117
|
+
|
|
118
|
+
// Logs & stats
|
|
119
|
+
const logs = await container.logs({ stdout: true, stderr: true, follow: true });
|
|
120
|
+
const stats = await container.stats({ stream: false });
|
|
121
|
+
|
|
122
|
+
// Exec
|
|
123
|
+
const exec = await container.exec({ Cmd: ["ls", "-la"], AttachStdout: true });
|
|
124
|
+
const output = await exec.start();
|
|
125
|
+
|
|
126
|
+
// Interactive attach (stdin support via hijack)
|
|
127
|
+
const duplex = await container.attach({ stream: true, stdin: true, stdout: true });
|
|
128
|
+
|
|
129
|
+
// Filesystem
|
|
130
|
+
await container.putArchive(tarStream, { path: "/app" });
|
|
131
|
+
const archive = await container.getArchive({ path: "/app" });
|
|
132
|
+
const archiveInfo = await container.infoArchive({ path: "/app" });
|
|
133
|
+
|
|
134
|
+
// Checkpoints
|
|
135
|
+
await container.listCheckpoints();
|
|
136
|
+
await container.createCheckpoint({ CheckpointID: "cp1", Exit: true });
|
|
137
|
+
await container.deleteCheckpoint("cp1");
|
|
138
|
+
|
|
139
|
+
// Other
|
|
140
|
+
await container.wait();
|
|
141
|
+
await container.commit({ repo: "myimage", tag: "v1" });
|
|
142
|
+
await container.top();
|
|
143
|
+
await container.changes();
|
|
144
|
+
await container.resize({ h: 40, w: 120 });
|
|
145
|
+
await container.update({ Memory: 512 * 1024 * 1024 });
|
|
146
|
+
await container.rename("new-name");
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
### Images
|
|
150
|
+
|
|
151
|
+
```ts
|
|
152
|
+
// List, inspect, search
|
|
153
|
+
const images = await docker.images.list();
|
|
154
|
+
const image = await docker.images.get("nginx:latest");
|
|
155
|
+
const info = await image.inspect();
|
|
156
|
+
const results = await docker.images.search({ term: "nginx" });
|
|
157
|
+
|
|
158
|
+
// Pull (convenience)
|
|
159
|
+
const stream = await docker.pull("nginx:1.25");
|
|
160
|
+
|
|
161
|
+
// Pull with auth
|
|
162
|
+
const stream = await docker.pull("private/image:v1", {
|
|
163
|
+
authconfig: { username: "user", password: "pass" },
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
// Build from tar context
|
|
167
|
+
const buildStream = await docker.images.build(tarStream, {
|
|
168
|
+
t: "myapp:latest",
|
|
169
|
+
dockerfile: "Dockerfile",
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
// Load / Import
|
|
173
|
+
const loadStream = await docker.images.load(tarStream);
|
|
174
|
+
const importStream = await docker.images.import(tarStream, { repo: "imported", tag: "v1" });
|
|
175
|
+
|
|
176
|
+
// Tag, push, remove
|
|
177
|
+
await image.tag({ repo: "myrepo/myimage", tag: "v2" });
|
|
178
|
+
await image.push({ authconfig: { username: "user", password: "pass" } });
|
|
179
|
+
await image.remove({ force: true });
|
|
180
|
+
await image.history();
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
### Volumes
|
|
184
|
+
|
|
185
|
+
```ts
|
|
186
|
+
const volumes = await docker.volumes.list();
|
|
187
|
+
const volume = await docker.volumes.create({ Name: "mydata", Driver: "local" });
|
|
188
|
+
const info = await volume.inspect();
|
|
189
|
+
await volume.remove();
|
|
190
|
+
await docker.volumes.prune();
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
### Networks
|
|
194
|
+
|
|
195
|
+
```ts
|
|
196
|
+
const networks = await docker.networks.list();
|
|
197
|
+
const network = await docker.networks.create({ Name: "mynet", Driver: "bridge" });
|
|
198
|
+
await network.connect({ Container: "abc123" });
|
|
199
|
+
await network.disconnect({ Container: "abc123" });
|
|
200
|
+
await network.remove();
|
|
201
|
+
await docker.networks.prune();
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
### System
|
|
205
|
+
|
|
206
|
+
```ts
|
|
207
|
+
const info = await docker.system.info();
|
|
208
|
+
const version = await docker.system.version();
|
|
209
|
+
const ping = await docker.system.ping();
|
|
210
|
+
const events = await docker.system.events({ since: "1h" });
|
|
211
|
+
const df = await docker.system.df();
|
|
212
|
+
const auth = await docker.system.auth({ username: "user", password: "pass" });
|
|
213
|
+
|
|
214
|
+
// Prune
|
|
215
|
+
await docker.system.pruneContainers();
|
|
216
|
+
await docker.system.pruneImages();
|
|
217
|
+
await docker.system.pruneVolumes();
|
|
218
|
+
await docker.system.pruneNetworks();
|
|
219
|
+
await docker.system.pruneBuilder({ all: true });
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
### Swarm (Services, Tasks, Nodes, Secrets, Configs)
|
|
223
|
+
|
|
224
|
+
```ts
|
|
225
|
+
// Services
|
|
226
|
+
const services = await docker.services.list();
|
|
227
|
+
const service = await docker.services.create({ Name: "web", TaskTemplate: { /* ... */ } });
|
|
228
|
+
await service.update({ version: 1, spec: { /* ... */ } });
|
|
229
|
+
await service.logs({ stdout: true });
|
|
230
|
+
await service.remove();
|
|
231
|
+
|
|
232
|
+
// Tasks, Nodes, Secrets, Configs follow the same pattern
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
### Plugins
|
|
236
|
+
|
|
237
|
+
```ts
|
|
238
|
+
const plugins = await docker.plugins.list();
|
|
239
|
+
await docker.plugins.install({ remote: "vieux/sshfs", name: "sshfs" });
|
|
240
|
+
const plugin = await docker.plugins.get("sshfs");
|
|
241
|
+
await plugin.enable();
|
|
242
|
+
await plugin.disable();
|
|
243
|
+
await plugin.remove();
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
## Stream Utilities
|
|
247
|
+
|
|
248
|
+
### demuxStream
|
|
249
|
+
|
|
250
|
+
Demultiplex Docker's multiplexed stdout/stderr stream:
|
|
251
|
+
|
|
252
|
+
```ts
|
|
253
|
+
import { demuxStream } from "@otterdeploy/docker";
|
|
254
|
+
|
|
255
|
+
const logs = await container.logs({ stdout: true, stderr: true, follow: true });
|
|
256
|
+
if (logs.isOk()) {
|
|
257
|
+
demuxStream(logs.value, process.stdout, process.stderr);
|
|
258
|
+
}
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
### followProgress
|
|
262
|
+
|
|
263
|
+
Parse newline-delimited JSON progress streams (from pull, push, build):
|
|
264
|
+
|
|
265
|
+
```ts
|
|
266
|
+
import { followProgress } from "@otterdeploy/docker";
|
|
267
|
+
|
|
268
|
+
const stream = await docker.pull("nginx:latest");
|
|
269
|
+
if (stream.isOk()) {
|
|
270
|
+
followProgress(
|
|
271
|
+
stream.value,
|
|
272
|
+
(err, output) => {
|
|
273
|
+
if (err) console.error(err);
|
|
274
|
+
else console.log("Done!", output.length, "events");
|
|
275
|
+
},
|
|
276
|
+
(event) => console.log(event),
|
|
277
|
+
);
|
|
278
|
+
}
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
## Error Handling
|
|
282
|
+
|
|
283
|
+
All API methods return `Result<T, DockerError>`. Errors are tagged by type:
|
|
284
|
+
|
|
285
|
+
```ts
|
|
286
|
+
import { DockerNotFoundError } from "@otterdeploy/docker";
|
|
287
|
+
|
|
288
|
+
const result = await container.inspect();
|
|
289
|
+
if (result.isErr()) {
|
|
290
|
+
const error = result.error;
|
|
291
|
+
if (error instanceof DockerNotFoundError) {
|
|
292
|
+
console.log("Container not found");
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
Error types: `DockerBadRequestError`, `DockerUnauthorizedError`, `DockerForbiddenError`, `DockerNotFoundError`, `DockerConflictError`, `DockerServerError`, `DockerServiceUnavailableError`, `DockerNetworkError`, `DockerTimeoutError`, `DockerAbortError`.
|
|
298
|
+
|
|
299
|
+
## BuildKit Sessions
|
|
300
|
+
|
|
301
|
+
For BuildKit v2 builds with registry authentication:
|
|
302
|
+
|
|
303
|
+
```ts
|
|
304
|
+
import { withSession } from "@otterdeploy/docker";
|
|
305
|
+
|
|
306
|
+
const session = await withSession(transport, "v1.53", {
|
|
307
|
+
username: "user",
|
|
308
|
+
password: "pass",
|
|
309
|
+
});
|
|
310
|
+
```
|
|
311
|
+
|
|
312
|
+
## Scripts
|
|
313
|
+
|
|
314
|
+
```bash
|
|
315
|
+
bun run build # Build with tsdown
|
|
316
|
+
bun test # Run tests
|
|
317
|
+
bun run lint # Lint with oxlint
|
|
318
|
+
bun run format # Format with oxfmt
|
|
319
|
+
bun run typecheck # Type check
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
## License
|
|
323
|
+
|
|
324
|
+
MIT
|