Skip to main content

bv_runtime/
docker.rs

1use std::collections::HashMap;
2use std::io::{BufRead, BufReader, Read};
3use std::process::{Command, Stdio};
4use std::thread;
5use std::time::Instant;
6
7use bv_core::error::{BvError, Result};
8
9use crate::runtime::{
10    ContainerRuntime, GpuProfile, ImageDigest, ImageMetadata, Mount, OciRef, ProgressReporter,
11    RunOutcome, RunSpec, RuntimeInfo,
12};
13
14#[derive(Clone)]
15pub struct DockerRuntime;
16
17impl ContainerRuntime for DockerRuntime {
18    fn name(&self) -> &str {
19        "docker"
20    }
21
22    fn health_check(&self) -> Result<RuntimeInfo> {
23        let output = Command::new("docker")
24            .arg("version")
25            .output()
26            .map_err(|e| BvError::RuntimeNotAvailable {
27                runtime: "docker".into(),
28                reason: format!("could not execute `docker`: {e}"),
29            })?;
30
31        if !output.status.success() {
32            let stderr = String::from_utf8_lossy(&output.stderr);
33            return Err(BvError::RuntimeNotAvailable {
34                runtime: "docker".into(),
35                reason: format!("docker daemon not running or not accessible: {stderr}"),
36            });
37        }
38
39        let stdout = String::from_utf8_lossy(&output.stdout);
40
41        // Parse "Version: X.Y.Z" lines; stable across Docker Engine and Desktop.
42        let versions: Vec<&str> = stdout
43            .lines()
44            .filter_map(|l| l.trim().strip_prefix("Version:").map(|v| v.trim()))
45            .collect();
46
47        let client_version = versions.first().copied().unwrap_or("unknown").to_string();
48        let server_version = versions.get(1).copied().map(str::to_string);
49
50        let mut extra = HashMap::new();
51        if let Some(sv) = server_version {
52            extra.insert("server_version".into(), sv);
53        }
54
55        Ok(RuntimeInfo {
56            name: "docker".into(),
57            version: client_version,
58            extra,
59        })
60    }
61
62    fn pull(&self, image: &OciRef, progress: &dyn ProgressReporter) -> Result<ImageDigest> {
63        let image_arg = image.docker_arg();
64        progress.update(&format!("Pulling {image_arg}"), None, None);
65
66        let mut child = Command::new("docker")
67            .args(["pull", &image_arg])
68            .stdout(Stdio::piped())
69            .stderr(Stdio::piped())
70            .spawn()
71            .map_err(|e| BvError::RuntimeNotAvailable {
72                runtime: "docker".into(),
73                reason: format!("could not execute `docker`: {e}"),
74            })?;
75
76        let stdout = child.stdout.take().expect("stdout was piped");
77        let stderr = child.stderr.take().expect("stderr was piped");
78
79        // Drain stderr in a background thread to prevent pipe deadlock.
80        let stderr_thread = thread::spawn(move || {
81            let mut s = String::new();
82            BufReader::new(stderr).read_to_string(&mut s).ok();
83            s
84        });
85
86        // Parse docker pull stdout line-by-line for progress and the digest.
87        let mut pull_digest: Option<String> = None;
88        for line in BufReader::new(stdout).lines() {
89            let line = line.map_err(BvError::Io)?;
90            let trimmed = line.trim();
91            // "Digest: sha256:abc123"
92            if let Some(d) = trimmed.strip_prefix("Digest: ") {
93                pull_digest = Some(d.to_string());
94            }
95            progress.update(trimmed, None, None);
96        }
97
98        let status = child.wait()?;
99        let stderr_output = stderr_thread.join().unwrap_or_default();
100
101        if !status.success() {
102            return Err(classify_pull_error(&stderr_output, &image_arg));
103        }
104
105        progress.finish(""); // outer "Added X" line is the summary; avoid redundant URL spam
106
107        let digest = match pull_digest {
108            Some(d) => d,
109            None => self.repo_digest(&image_arg)?,
110        };
111
112        Ok(ImageDigest(digest))
113    }
114
115    fn run(&self, spec: &RunSpec) -> Result<RunOutcome> {
116        let start = Instant::now();
117
118        let mut cmd = Command::new("docker");
119        cmd.arg("run").arg("--rm");
120
121        // Run as the host user so files written into bind mounts are not
122        // owned by root. On non-Unix platforms `current_uid_gid` returns
123        // None and we let docker pick the image default.
124        if let Some((uid, gid)) = current_uid_gid() {
125            cmd.args(["--user", &format!("{uid}:{gid}")]);
126        }
127
128        if let Some(wd) = &spec.working_dir {
129            cmd.args(["-w", &wd.to_string_lossy()]);
130        }
131
132        for arg in self.mount_args(&spec.mounts) {
133            cmd.arg(arg);
134        }
135
136        for (k, v) in &spec.env {
137            cmd.arg("-e").arg(format!("{k}={v}"));
138        }
139
140        for arg in self.gpu_args(&spec.gpu) {
141            cmd.arg(arg);
142        }
143
144        // Pass NVIDIA_VISIBLE_DEVICES through if the user has set it.
145        if let Ok(val) = std::env::var("NVIDIA_VISIBLE_DEVICES") {
146            cmd.arg("-e").arg(format!("NVIDIA_VISIBLE_DEVICES={val}"));
147        }
148
149        cmd.arg(spec.image.docker_arg());
150
151        for arg in &spec.command {
152            cmd.arg(arg);
153        }
154
155        if spec.capture_output {
156            cmd.stdin(Stdio::null())
157                .stdout(Stdio::piped())
158                .stderr(Stdio::piped());
159            let output = cmd
160                .output()
161                .map_err(|e| BvError::RuntimeError(format!("docker run failed to launch: {e}")))?;
162            return Ok(RunOutcome {
163                exit_code: output.status.code().unwrap_or(-1),
164                duration: start.elapsed(),
165                stdout: output.stdout,
166                stderr: output.stderr,
167            });
168        }
169
170        cmd.stdin(Stdio::inherit())
171            .stdout(Stdio::inherit())
172            .stderr(Stdio::inherit());
173
174        let status = cmd
175            .status()
176            .map_err(|e| BvError::RuntimeError(format!("docker run failed to launch: {e}")))?;
177
178        Ok(RunOutcome {
179            exit_code: status.code().unwrap_or(-1),
180            duration: start.elapsed(),
181            stdout: Vec::new(),
182            stderr: Vec::new(),
183        })
184    }
185
186    fn inspect(&self, digest: &ImageDigest) -> Result<ImageMetadata> {
187        let output = Command::new("docker")
188            .args(["image", "inspect", "--format", "{{.Size}}", &digest.0])
189            .output()
190            .map_err(|e| BvError::RuntimeError(e.to_string()))?;
191
192        if !output.status.success() {
193            return Err(BvError::RuntimeError(format!(
194                "docker image inspect failed for '{}'",
195                digest.0
196            )));
197        }
198
199        let size_bytes = String::from_utf8_lossy(&output.stdout)
200            .trim()
201            .parse::<u64>()
202            .ok();
203
204        Ok(ImageMetadata {
205            digest: digest.clone(),
206            size_bytes,
207            labels: HashMap::new(),
208        })
209    }
210
211    fn is_locally_available(&self, image_ref: &str, digest: &str) -> bool {
212        let pinned = format!("{image_ref}@{digest}");
213        Command::new("docker")
214            .args(["image", "inspect", "--format", "{{.Id}}", &pinned])
215            .stdout(Stdio::null())
216            .stderr(Stdio::null())
217            .status()
218            .map(|s| s.success())
219            .unwrap_or(false)
220    }
221
222    fn gpu_args(&self, profile: &GpuProfile) -> Vec<String> {
223        match &profile.spec {
224            Some(spec) if spec.required => vec!["--gpus".into(), "all".into()],
225            _ => vec![],
226        }
227    }
228
229    fn mount_args(&self, mounts: &[Mount]) -> Vec<String> {
230        mounts
231            .iter()
232            .flat_map(|m| {
233                let mode = if m.read_only { "ro" } else { "rw" };
234                let spec = format!(
235                    "{}:{}:{mode}",
236                    m.host_path.display(),
237                    m.container_path.display()
238                );
239                ["-v".to_string(), spec]
240            })
241            .collect()
242    }
243}
244
245impl DockerRuntime {
246    /// Pull `image` and bail if the registry-reported digest does not match
247    /// `expected_digest`.
248    ///
249    /// `pull` itself does not enforce this: it returns whatever digest the
250    /// registry hands back. Most callers (`bv sync`) already cross-check
251    /// against the lockfile, but the `bv run` and `bv conform` paths short
252    /// circuit through `is_locally_available`, which only proves that a
253    /// matching RepoDigests entry exists in the local cache, not that the
254    /// upstream image still resolves to the pinned sha. New code that
255    /// requires a digest pin should call this method instead of `pull`.
256    //
257    // TODO: route `bv run` / `bv conform` through this once the
258    // `ContainerRuntime` trait gains a `pull_verified` method (would
259    // touch bv-cli/runtime_select and the apptainer impl, so deferred).
260    pub fn pull_verified(
261        &self,
262        image: &OciRef,
263        expected_digest: &str,
264        progress: &dyn ProgressReporter,
265    ) -> Result<ImageDigest> {
266        let got = self.pull(image, progress)?;
267        verify_digest(&image.to_string(), expected_digest, &got.0)?;
268        Ok(got)
269    }
270
271    /// Get the content digest for a locally available image by reference.
272    fn repo_digest(&self, image_ref: &str) -> Result<String> {
273        let output = Command::new("docker")
274            .args([
275                "image",
276                "inspect",
277                "--format",
278                "{{index .RepoDigests 0}}",
279                image_ref,
280            ])
281            .output()
282            .map_err(|e| BvError::RuntimeError(e.to_string()))?;
283
284        if !output.status.success() {
285            return Err(BvError::RuntimeError(format!(
286                "could not inspect image '{image_ref}' after pull"
287            )));
288        }
289
290        let line = String::from_utf8_lossy(&output.stdout);
291        let line = line.trim();
292
293        // RepoDigests entry looks like "ncbi/blast@sha256:abc123"; extract digest part.
294        if let Some(digest) = line.split('@').nth(1) {
295            Ok(digest.to_string())
296        } else if line.starts_with("sha256:") {
297            Ok(line.to_string())
298        } else {
299            // Locally built image without a registry digest; fall back to image ID.
300            let id_output = Command::new("docker")
301                .args(["image", "inspect", "--format", "{{.Id}}", image_ref])
302                .output()
303                .map_err(|e| BvError::RuntimeError(e.to_string()))?;
304            Ok(String::from_utf8_lossy(&id_output.stdout)
305                .trim()
306                .to_string())
307        }
308    }
309}
310
311/// Return the calling process's effective uid:gid on Unix.
312///
313/// Falls back to `None` on non-Unix targets (Windows) so callers can skip
314/// passing `--user` rather than guessing.
315#[cfg(unix)]
316fn current_uid_gid() -> Option<(u32, u32)> {
317    // SAFETY: getuid()/getgid() are async-signal-safe and never fail per POSIX.
318    unsafe extern "C" {
319        fn getuid() -> u32;
320        fn getgid() -> u32;
321    }
322    Some((unsafe { getuid() }, unsafe { getgid() }))
323}
324
325#[cfg(not(unix))]
326fn current_uid_gid() -> Option<(u32, u32)> {
327    None
328}
329
330/// Compare a registry-returned digest to the digest the caller pinned and
331/// return a clear error on mismatch.
332fn verify_digest(image_ref: &str, expected: &str, got: &str) -> Result<()> {
333    if expected == got {
334        Ok(())
335    } else {
336        Err(BvError::RuntimeError(format!(
337            "image digest mismatch for '{image_ref}': expected {expected} but registry returned {got}"
338        )))
339    }
340}
341
342/// Map docker pull stderr to a user-friendly `BvError`.
343fn classify_pull_error(stderr: &str, image_ref: &str) -> BvError {
344    if stderr.contains("Cannot connect to the Docker daemon")
345        || stderr.contains("Is the docker daemon running")
346    {
347        BvError::RuntimeNotAvailable {
348            runtime: "docker".into(),
349            reason: "Docker daemon is not available. Is Docker Desktop running?".into(),
350        }
351    } else if stderr.contains("manifest unknown")
352        || stderr.contains("not found")
353        || stderr.contains("does not exist")
354    {
355        BvError::RuntimeError(format!(
356            "image '{image_ref}' not found in registry (check the tool manifest)"
357        ))
358    } else if stderr.contains("connection refused") || stderr.contains("no such host") {
359        BvError::RuntimeError(format!(
360            "network error while pulling '{image_ref}': {stderr}"
361        ))
362    } else {
363        BvError::RuntimeError(format!("docker pull failed:\n{stderr}"))
364    }
365}
366
367#[cfg(test)]
368mod tests {
369    use super::*;
370
371    #[test]
372    fn verify_digest_matches() {
373        assert!(verify_digest("ncbi/blast", "sha256:abc", "sha256:abc").is_ok());
374    }
375
376    #[test]
377    fn verify_digest_rejects_mismatch() {
378        let err = verify_digest("ncbi/blast", "sha256:abc", "sha256:def").unwrap_err();
379        let msg = err.to_string();
380        assert!(msg.contains("ncbi/blast"));
381        assert!(msg.contains("sha256:abc"));
382        assert!(msg.contains("sha256:def"));
383        assert!(msg.contains("mismatch"));
384    }
385
386    #[cfg(unix)]
387    #[test]
388    fn current_uid_gid_returns_some_on_unix() {
389        let got = current_uid_gid();
390        assert!(got.is_some());
391    }
392}