1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
//! Thin wrapper around bollard for the local executor.
//!
//! Operations: pull images, start containers (long-lived sleep), exec
//! commands streaming stdout/stderr, commit container to image, look
//! up images by tag, stop and remove containers.
use std::collections::HashMap;
use std::sync::Arc;
use anyhow::{Context, Result};
use bollard::Docker;
use bollard::container::{
Config, CreateContainerOptions, RemoveContainerOptions, StartContainerOptions,
StopContainerOptions,
};
use bollard::exec::{CreateExecOptions, StartExecResults};
use bollard::image::{
CommitContainerOptions, CreateImageOptions, ImportImageOptions, ListImagesOptions,
RemoveImageOptions, TagImageOptions,
};
use bollard::models::HostConfig;
use futures_util::StreamExt;
use tokio::io::AsyncWrite;
use crate::error::HmError;
/// Build a [`HostConfig`] with optional bind mounts and Linux capabilities.
///
/// Empty slices become `None` so Docker applies its defaults.
fn build_host_config(binds: &[String], cap_add: &[String]) -> HostConfig {
HostConfig {
binds: if binds.is_empty() {
None
} else {
Some(binds.to_vec())
},
cap_add: if cap_add.is_empty() {
None
} else {
Some(cap_add.to_vec())
},
..Default::default()
}
}
#[derive(Debug, Clone)]
pub struct DockerClient {
inner: Arc<Docker>,
}
impl DockerClient {
/// Open a Docker connection using the platform's default socket /
/// pipe. The handle is cheap to clone (refcounted internally).
///
/// # Errors
///
/// Returns [`HmError::Docker`] when bollard cannot resolve a
/// local Docker endpoint (no socket on `DOCKER_HOST`, no Windows
/// pipe, etc.).
pub fn connect() -> Result<Self> {
let d = Docker::connect_with_local_defaults()
.map_err(|e| HmError::Docker(format!("connect: {e}")))?;
Ok(Self { inner: Arc::new(d) })
}
/// Round-trip the daemon to confirm reachability.
///
/// # Errors
///
/// Returns [`HmError::Docker`] if the ping request fails (daemon
/// stopped, socket revoked, version negotiation failure).
pub async fn ping(&self) -> Result<()> {
self.inner
.ping()
.await
.map_err(|e| HmError::Docker(format!("ping failed: {e}")))?;
Ok(())
}
/// True if `tag` resolves to a locally-cached image.
///
/// # Errors
///
/// Returns [`HmError::Docker`] if the `list_images` API call
/// fails (daemon unreachable, malformed filter).
pub async fn image_exists(&self, tag: &str) -> Result<bool> {
let mut filters = HashMap::new();
filters.insert("reference".to_string(), vec![tag.to_string()]);
let images = self
.inner
.list_images(Some(ListImagesOptions {
filters,
..Default::default()
}))
.await
.map_err(|e| HmError::Docker(format!("list_images: {e}")))?;
Ok(!images.is_empty())
}
/// List all `repo_tags` from images that have at least one tag
/// matching `reference` (e.g. `"harmont-local/build"` matches
/// `harmont-local/build:abc123`).
///
/// # Errors
///
/// Returns [`HmError::Docker`] if the `list_images` API call fails.
pub async fn list_images_by_reference(&self, reference: &str) -> Result<Vec<String>> {
let mut filters = HashMap::new();
filters.insert("reference".to_string(), vec![format!("{reference}:*")]);
let images = self
.inner
.list_images(Some(ListImagesOptions {
filters,
..Default::default()
}))
.await
.map_err(|e| HmError::Docker(format!("list_images: {e}")))?;
Ok(images.into_iter().flat_map(|img| img.repo_tags).collect())
}
/// Pull `tag` from its registry, surfacing the daemon's progress
/// stream as Docker errors.
///
/// # Errors
///
/// Returns [`HmError::Docker`] if any chunk of the pull stream
/// reports an error (registry not reachable, image not found,
/// auth required).
pub async fn pull_image(&self, tag: &str) -> Result<()> {
let mut s = self.inner.create_image(
Some(CreateImageOptions {
from_image: tag,
..Default::default()
}),
None,
None,
);
while let Some(item) = s.next().await {
item.map_err(|e| HmError::Docker(format!("pull {tag}: {e}")))?;
}
Ok(())
}
/// Start a long-lived container that runs `sh -c 'sleep infinity'` so
/// later `exec`s land in a stable shell. Returns the container ID.
///
/// # Errors
///
/// Returns [`HmError::Docker`] if the container cannot be created
/// (image not pulled, name conflict, OCI runtime failure) or if
/// `start_container` rejects the create.
pub async fn start_long_lived(
&self,
image: &str,
env: &[String],
workdir: &str,
name: &str,
) -> Result<String> {
self.start_long_lived_with_mounts(image, env, workdir, name, &[])
.await
}
/// Like [`Self::start_long_lived`] but with bind mounts via `HostConfig`.
///
/// Each entry in `binds` is a Docker bind-mount string of the form
/// `"/host/path:/container/path"` (with an optional `:ro` suffix).
///
/// # Errors
///
/// Returns [`HmError::Docker`] if the container cannot be created
/// (image not pulled, name conflict, OCI runtime failure) or if
/// `start_container` rejects the create.
pub async fn start_long_lived_with_mounts(
&self,
image: &str,
env: &[String],
workdir: &str,
name: &str,
binds: &[String],
) -> Result<String> {
let cfg = Config {
image: Some(image.to_string()),
cmd: Some(vec!["sh".into(), "-c".into(), "sleep infinity".into()]),
env: Some(env.to_vec()),
working_dir: Some(workdir.to_string()),
host_config: Some(build_host_config(binds, &[])),
..Default::default()
};
let create = self
.inner
.create_container(
Some(CreateContainerOptions {
name,
..Default::default()
}),
cfg,
)
.await
.map_err(|e| HmError::Docker(format!("create_container: {e}")))?;
self.inner
.start_container(&create.id, None::<StartContainerOptions<String>>)
.await
.map_err(|e| HmError::Docker(format!("start_container: {e}")))?;
Ok(create.id)
}
/// Exec a command inside a running container and stream stdout+stderr
/// to `out`. Returns the command's exit code.
///
/// # Errors
///
/// Returns [`HmError::Docker`] if `create_exec` / `start_exec` /
/// `inspect_exec` fail, or surfaces an `anyhow` error if writing a
/// log frame to `out` fails.
pub async fn exec_streaming(
&self,
container_id: &str,
cmd: &[String],
env: &[String],
workdir: &str,
out: &mut (impl AsyncWrite + Send + Unpin),
) -> Result<i64> {
use bollard::container::LogOutput;
use tokio::io::AsyncWriteExt;
let exec = self
.inner
.create_exec(
container_id,
CreateExecOptions {
cmd: Some(cmd.iter().map(std::string::String::as_str).collect()),
env: Some(env.iter().map(std::string::String::as_str).collect()),
working_dir: Some(workdir),
attach_stdout: Some(true),
attach_stderr: Some(true),
..Default::default()
},
)
.await
.map_err(|e| HmError::Docker(format!("create_exec: {e}")))?;
match self
.inner
.start_exec(&exec.id, None)
.await
.map_err(|e| HmError::Docker(format!("start_exec: {e}")))?
{
StartExecResults::Attached { mut output, .. } => {
while let Some(item) = output.next().await {
let chunk = item.map_err(|e| HmError::Docker(format!("exec stream: {e}")))?;
let (LogOutput::StdOut { message: bytes }
| LogOutput::StdErr { message: bytes }
| LogOutput::Console { message: bytes }) = chunk
else {
// StdIn frames are echoed by some daemons; ignore them.
continue;
};
out.write_all(&bytes).await.context("write exec output")?;
}
}
StartExecResults::Detached => {}
}
let inspect = self
.inner
.inspect_exec(&exec.id)
.await
.map_err(|e| HmError::Docker(format!("inspect_exec: {e}")))?;
Ok(inspect.exit_code.unwrap_or(0))
}
/// Like [`Self::exec_streaming`], but also pipes `stdin_bytes` into the
/// exec'd process's stdin (closing it after the write so the process
/// sees EOF). Used to stream a tar archive into `tar -xzf -` when
/// hydrating `/workspace` in a fresh chain-root container.
///
/// # Errors
///
/// Returns [`HmError::Docker`] if any of the exec lifecycle calls
/// fail, or surfaces an `anyhow` error if writing stdin or output
/// frames fails.
pub async fn exec_streaming_stdin(
&self,
container_id: &str,
cmd: &[String],
env: &[String],
workdir: &str,
stdin_bytes: &[u8],
out: &mut (impl AsyncWrite + Send + Unpin),
) -> Result<i64> {
use bollard::container::LogOutput;
use tokio::io::AsyncWriteExt;
let exec = self
.inner
.create_exec(
container_id,
CreateExecOptions {
cmd: Some(cmd.iter().map(std::string::String::as_str).collect()),
env: Some(env.iter().map(std::string::String::as_str).collect()),
working_dir: Some(workdir),
attach_stdin: Some(true),
attach_stdout: Some(true),
attach_stderr: Some(true),
..Default::default()
},
)
.await
.map_err(|e| HmError::Docker(format!("create_exec: {e}")))?;
match self
.inner
.start_exec(&exec.id, None)
.await
.map_err(|e| HmError::Docker(format!("start_exec: {e}")))?
{
StartExecResults::Attached {
mut output,
mut input,
} => {
input
.write_all(stdin_bytes)
.await
.context("write exec stdin")?;
input.shutdown().await.context("close exec stdin")?;
// Drop the writer to fully release the half-duplex.
drop(input);
while let Some(item) = output.next().await {
let chunk = item.map_err(|e| HmError::Docker(format!("exec stream: {e}")))?;
let (LogOutput::StdOut { message: bytes }
| LogOutput::StdErr { message: bytes }
| LogOutput::Console { message: bytes }) = chunk
else {
// StdIn frames are echoed by some daemons; ignore them.
continue;
};
out.write_all(&bytes).await.context("write exec output")?;
}
}
StartExecResults::Detached => {}
}
let inspect = self
.inner
.inspect_exec(&exec.id)
.await
.map_err(|e| HmError::Docker(format!("inspect_exec: {e}")))?;
Ok(inspect.exit_code.unwrap_or(0))
}
/// Commit a running container to an image tag. Returns the tag, which
/// is a valid image reference once the daemon's commit succeeds.
///
/// We don't return the daemon's image ID: bollard 0.18's `Commit`
/// stub deserialises the response as `{"id": ...}`, but the Docker
/// daemon returns `{"Id": ...}` (capital I). The image is committed
/// correctly either way; the tag is the canonical reference and is
/// what every caller actually uses.
///
/// # Errors
///
/// Returns [`HmError::Docker`] if `commit_container` fails (paused
/// container, daemon I/O failure).
///
/// # Panics
///
/// Panics if `tag.splitn(2, ':')` produces neither one nor two parts.
/// `splitn` is total for non-empty input, so this branch is only
/// reachable for the empty string, which the caller never passes.
pub async fn commit_container(&self, container_id: &str, tag: &str) -> Result<String> {
let parts: Vec<&str> = tag.splitn(2, ':').collect();
let (repo, ver) = match parts.as_slice() {
[r, v] => (*r, *v),
[r] => (*r, "latest"),
_ => unreachable!("splitn(2) yields one or two parts for non-empty input"),
};
let opts = CommitContainerOptions {
container: container_id,
repo,
tag: ver,
..Default::default()
};
self.inner
.commit_container(opts, Config::<String>::default())
.await
.map_err(|e| HmError::Docker(format!("commit_container: {e}")))?;
Ok(tag.to_string())
}
/// Force-remove an image by tag. Used for end-of-run pruning of
/// ephemeral parent-snapshot tags committed during this process's
/// run. Best-effort callers should swallow the error themselves;
/// failures here are non-fatal.
///
/// # Errors
///
/// Returns [`HmError::Docker`] if `remove_image` fails (image
/// missing, still referenced by a running container, daemon I/O
/// failure).
pub async fn remove_image(&self, image: &str) -> Result<()> {
self.inner
.remove_image(
image,
Some(RemoveImageOptions {
force: true,
noprune: false,
}),
None,
)
.await
.map_err(|e| HmError::Docker(format!("remove_image '{image}': {e}")))?;
Ok(())
}
/// Add an additional tag to an existing image.
///
/// `source` is the existing image reference (tag or ID) and
/// `new_tag` is the desired `repo:tag` string. If `new_tag`
/// contains no `:`, the Docker-default tag `"latest"` is used.
///
/// # Errors
///
/// Returns [`HmError::Docker`] if `tag_image` fails (source image
/// not found, daemon I/O failure).
pub async fn tag_image(&self, source: &str, new_tag: &str) -> Result<()> {
let parts: Vec<&str> = new_tag.splitn(2, ':').collect();
let (repo, tag) = match parts.as_slice() {
[r, v] => (*r, *v),
[r] => (*r, "latest"),
_ => unreachable!("splitn(2) yields one or two parts for non-empty input"),
};
self.inner
.tag_image(source, Some(TagImageOptions { repo, tag }))
.await
.map_err(|e| HmError::Docker(format!("tag_image '{source}' -> '{new_tag}': {e}")))?;
Ok(())
}
/// Export a Docker image to a tar file on disk.
///
/// Streams the image layer data from the daemon and writes it to
/// `dest` using a buffered writer.
///
/// # Errors
///
/// Returns [`HmError::Docker`] if the daemon's export stream fails,
/// or an I/O error if writing to `dest` fails.
pub async fn export_image(&self, image: &str, dest: &std::path::Path) -> Result<()> {
use tokio::io::AsyncWriteExt;
let mut stream = self.inner.export_image(image);
let file = tokio::fs::File::create(dest)
.await
.with_context(|| format!("create export file '{}'", dest.display()))?;
let mut writer = tokio::io::BufWriter::new(file);
while let Some(chunk) = stream.next().await {
let bytes =
chunk.map_err(|e| HmError::Docker(format!("export_image '{image}': {e}")))?;
writer
.write_all(&bytes)
.await
.with_context(|| format!("write export data to '{}'", dest.display()))?;
}
writer
.flush()
.await
.with_context(|| format!("flush export file '{}'", dest.display()))?;
Ok(())
}
/// Import a Docker image from a tar file on disk.
///
/// Reads the full tar file into memory and loads it into the
/// daemon via the image import API.
///
/// # Errors
///
/// Returns [`HmError::Docker`] if the daemon rejects the import
/// stream, or an I/O error if reading `src` fails.
pub async fn import_image(&self, src: &std::path::Path) -> Result<()> {
let body = tokio::fs::read(src)
.await
.with_context(|| format!("read import file '{}'", src.display()))?;
let mut stream =
self.inner
.import_image(ImportImageOptions { quiet: true }, body.into(), None);
while let Some(item) = stream.next().await {
item.map_err(|e| HmError::Docker(format!("import_image '{}': {e}", src.display())))?;
}
Ok(())
}
/// List all image tags whose name starts with `prefix`.
///
/// Uses the Docker `reference` filter with a glob pattern and then
/// post-filters the returned `repo_tags` to those that truly begin
/// with `prefix`. The result is sorted lexicographically.
///
/// # Errors
///
/// Returns [`HmError::Docker`] if the `list_images` API call
/// fails (daemon unreachable, malformed filter).
pub async fn list_images_by_prefix(&self, prefix: &str) -> Result<Vec<String>> {
let mut filters = HashMap::new();
filters.insert("reference".to_string(), vec![format!("{prefix}*")]);
let images = self
.inner
.list_images(Some(ListImagesOptions {
filters,
..Default::default()
}))
.await
.map_err(|e| HmError::Docker(format!("list_images: {e}")))?;
let mut tags: Vec<String> = images
.iter()
.flat_map(|img| &img.repo_tags)
.filter(|tag| tag.starts_with(prefix))
.cloned()
.collect();
tags.sort();
Ok(tags)
}
pub async fn stop_remove(&self, container_id: &str) {
let _ = self
.inner
.stop_container(container_id, Some(StopContainerOptions { t: 0 }))
.await;
let _ = self
.inner
.remove_container(
container_id,
Some(RemoveContainerOptions {
force: true,
v: true,
..Default::default()
}),
)
.await;
}
/// Internal access to the underlying bollard handle, for callers
/// that need to call bollard APIs not yet wrapped here (e.g., log
/// streaming via `Docker::logs`).
///
/// Prefer adding a dedicated method to this type; only use this
/// accessor when a one-off stream is needed outside the main
/// `DockerClient` API surface.
#[doc(hidden)]
#[must_use]
pub fn inner_for_logs(&self) -> &bollard::Docker {
&self.inner
}
/// List container summaries filtered by a single label `k=v` predicate.
///
/// # Errors
///
/// Returns [`HmError::Docker`] when `list_containers` fails.
pub async fn list_containers_by_label(
&self,
k: &str,
v: &str,
) -> Result<Vec<bollard::secret::ContainerSummary>> {
use bollard::container::ListContainersOptions;
use std::collections::HashMap;
let mut filters: HashMap<String, Vec<String>> = HashMap::new();
filters.insert("label".to_string(), vec![format!("{k}={v}")]);
let out = self
.inner
.list_containers(Some(ListContainersOptions {
all: true,
filters,
..Default::default()
}))
.await
.map_err(|e| HmError::Docker(format!("list_containers: {e}")))?;
Ok(out)
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
mod smoke {
use super::*;
#[tokio::test]
#[ignore = "requires a running Docker daemon; opt in with `cargo test -- --ignored`"]
async fn docker_ping() {
let c = DockerClient::connect().unwrap();
c.ping().await.unwrap();
}
#[tokio::test]
#[ignore = "requires a running Docker daemon; opt in with `cargo test -- --ignored`"]
async fn list_images_by_reference_returns_empty_for_nonexistent() {
let c = DockerClient::connect().unwrap();
let tags = c
.list_images_by_reference("harmont-test-nonexistent")
.await
.unwrap();
assert!(tags.is_empty());
}
#[test]
fn build_host_config_with_binds_and_no_caps() {
let hc = super::build_host_config(&["/host/path:/container/path".to_string()], &[]);
assert_eq!(
hc.binds.as_ref().unwrap(),
&["/host/path:/container/path".to_string()]
);
assert!(hc.cap_add.is_none());
}
#[test]
fn build_host_config_empty_binds_is_none() {
let hc = super::build_host_config(&[], &[]);
assert!(hc.binds.is_none());
assert!(hc.cap_add.is_none());
}
}