arcbox-docker 0.6.3

Docker REST API compatibility layer for ArcBox
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
use super::{extract_container_id, proxy_to_system_vm, require_amd64_runtime};
use crate::api::AppState;
use crate::error::{DockerError, Result};
use crate::port_bindings::parse_port_bindings;
use crate::routing::{query_param, route_container_create};
use axum::body::Body;
use axum::extract::{OriginalUri, State};
use axum::http::{Request, Uri};
use axum::response::Response;
use bytes::Bytes;
use std::net::IpAddr;

/// Create a container, resolving macOS symlinks in bind-mount source paths.
///
/// On macOS, `/tmp` → `/private/tmp` and `/var` → `/private/var`. The guest
/// mounts host `/private` via VirtioFS while its `/tmp` and `/var` are
/// isolated tmpfs. This handler resolves the top-level symlink so
/// bind-mount paths land on the VirtioFS share.
///
/// ABX-375: every runtime container runs in the single HV system VM.
/// `linux/amd64` is executed via FEX inside that VM; if FEX is not
/// provisioned in the guest, the request fails closed with a clear error
/// rather than silently routing to VZ/Rosetta or QEMU. Compose projects need
/// no cross-VM scheduling — all services share the one HV VM.
///
/// On a successful response the canonical container ID (and any `--name`) is
/// recorded so follow-up lifecycle calls resolve to the same VM.
#[tracing::instrument(
    name = "docker.container.create",
    skip(state, req),
    fields(
        uri = %uri,
        utility_vm = "native",
        translator = tracing::field::Empty,
        container_id = tracing::field::Empty,
        name = tracing::field::Empty,
    ),
    err
)]
pub async fn create_container(
    State(state): State<AppState>,
    OriginalUri(uri): OriginalUri,
    req: Request<Body>,
) -> Result<Response> {
    let (parts, body) = req.into_parts();
    let body_bytes = http_body_util::BodyExt::collect(body)
        .await
        .map_err(|e| DockerError::Server(format!("failed to read body: {e}")))?
        .to_bytes();

    let body_bytes = crate::host_path::rewrite_create_body(body_bytes);
    let route = route_container_create(&uri, &body_bytes);
    let requested_name = query_param(&uri, "name").map(str::to_string);
    tracing::Span::current().record("translator", route.translator.as_str());
    if let Some(name) = requested_name.as_deref() {
        tracing::Span::current().record("name", name);
    }

    // Fail closed: amd64 runtime requires FEX in the HV guest. Never fall
    // back to a VZ/Rosetta runtime VM for a default amd64 container.
    require_amd64_runtime(&state, route).await?;

    tracing::debug!(
        backend = "hv",
        translator = route.translator.as_str(),
        platform = ?route.platform,
        name = requested_name.as_deref().unwrap_or(""),
        "routing Docker container create request"
    );
    let mut req = Request::from_parts(parts, Body::from(body_bytes));
    // Body may have changed size; remove Content-Length so the proxy
    // recomputes framing from the actual body.
    req.headers_mut().remove(axum::http::header::CONTENT_LENGTH);

    proxy_to_system_vm(&state, &uri, req).await
}

/// Start a container, then set up host-side port forwarding and DNS.
///
/// # Errors
///
/// Returns an error if VM readiness fails or proxying to guest dockerd fails.
#[tracing::instrument(
    name = "docker.container.start",
    skip(state, req),
    fields(uri = %uri, utility_vm = "native", container_id = tracing::field::Empty),
    err
)]
pub async fn start_container(
    State(state): State<AppState>,
    OriginalUri(uri): OriginalUri,
    req: Request<Body>,
) -> Result<Response> {
    let container_id = extract_container_id(&uri);
    if let Some(id) = container_id.as_deref() {
        tracing::Span::current().record("container_id", id);
    }

    // Proxy start request to guest.
    let response = proxy_to_system_vm(&state, &uri, req).await?;

    // On success, inspect the container and set up port forwarding + DNS.
    if response.status().is_success() {
        if let Some(ref id) = container_id {
            setup_container_networking(&state, id).await;
        }
    }

    Ok(response)
}

/// Stop a container and tear down its port forwarding + DNS.
///
/// # Errors
///
/// Returns an error if VM readiness fails or proxying to guest dockerd fails.
#[tracing::instrument(
    name = "docker.container.stop",
    skip(state, req),
    fields(uri = %uri, utility_vm = "native", container_id = tracing::field::Empty),
    err
)]
pub async fn stop_container(
    State(state): State<AppState>,
    OriginalUri(uri): OriginalUri,
    req: Request<Body>,
) -> Result<Response> {
    if let Some(id) = extract_container_id(&uri) {
        tracing::Span::current().record("container_id", id.as_str());
    }
    // Resolve the canonical ID from the host registry (no guest round-trip).
    let canonical = resolve_or_raw_for_teardown(&state, &uri).await;

    let response = proxy_to_system_vm(&state, &uri, req).await?;

    // Tear down networking after a terminal stop response.
    // Docker returns 204 on success and 304 when already stopped.
    let status = response.status().as_u16();
    if status == 204 || status == 304 {
        if let Some(canonical) = canonical {
            state.runtime.stop_port_forwarding_by_id(&canonical).await;
            state.runtime.deregister_dns_by_id(&canonical).await;
        }
    }

    Ok(response)
}

/// Whether a `POST /containers/{id}/kill?signal=…` request terminates the
/// container, so its host networking should be torn down.
///
/// Docker returns 204 for *any* delivered signal — e.g. `SIGHUP` to reload
/// nginx or `SIGUSR1` — not just fatal ones, so teardown must key off the
/// signal, not the 204. Only the default (no `signal` = SIGKILL) and an
/// explicit SIGKILL are guaranteed to stop the container; anything else is
/// treated as non-terminating (SIGTERM may be caught; SIGHUP/SIGUSR* are
/// reload/notify signals). If such a signal does end up killing the container,
/// the death-event teardown path removes its host state instead.
fn kill_terminates_container(uri: &Uri) -> bool {
    match query_param(uri, "signal") {
        None => true, // Docker's default kill signal is SIGKILL.
        Some(signal) => {
            let signal = signal.trim();
            signal.eq_ignore_ascii_case("SIGKILL")
                || signal.eq_ignore_ascii_case("KILL")
                || signal == "9"
        }
    }
}

/// Kill a container and tear down its port forwarding + DNS.
///
/// # Errors
///
/// Returns an error if VM readiness fails or proxying to guest dockerd fails.
#[tracing::instrument(
    name = "docker.container.kill",
    skip(state, req),
    fields(uri = %uri, utility_vm = "native", container_id = tracing::field::Empty),
    err
)]
pub async fn kill_container(
    State(state): State<AppState>,
    OriginalUri(uri): OriginalUri,
    req: Request<Body>,
) -> Result<Response> {
    if let Some(id) = extract_container_id(&uri) {
        tracing::Span::current().record("container_id", id.as_str());
    }
    // Resolve the canonical ID from the host registry (no guest round-trip).
    let canonical = resolve_or_raw_for_teardown(&state, &uri).await;

    let terminates = kill_terminates_container(&uri);
    let response = proxy_to_system_vm(&state, &uri, req).await?;

    // Docker kill returns 204 for any delivered signal; only tear down when the
    // signal actually terminates the container (see `kill_terminates_container`).
    if response.status().as_u16() == 204 && terminates {
        if let Some(canonical) = canonical {
            state.runtime.stop_port_forwarding_by_id(&canonical).await;
            state.runtime.deregister_dns_by_id(&canonical).await;
        }
    }

    Ok(response)
}

/// Restart a container and refresh its DNS entry.
///
/// # Errors
///
/// Returns an error if VM readiness fails or proxying to guest dockerd fails.
#[tracing::instrument(
    name = "docker.container.restart",
    skip(state, req),
    fields(uri = %uri, utility_vm = "native", container_id = tracing::field::Empty),
    err
)]
pub async fn restart_container(
    State(state): State<AppState>,
    OriginalUri(uri): OriginalUri,
    req: Request<Body>,
) -> Result<Response> {
    if let Some(id) = extract_container_id(&uri) {
        tracing::Span::current().record("container_id", id.as_str());
    }
    let response = proxy_to_system_vm(&state, &uri, req).await?;

    // Docker restart returns 204 on success. Refresh DNS (container may get
    // a new IP) with a single inspect call for both canonical ID and DNS info.
    // Port forwarding targets the guest VM and survives container restarts.
    if response.status().as_u16() == 204 {
        if let Some(id) = extract_container_id(&uri) {
            let _ = state.runtime.ensure_vm_ready().await;
            if let Some(body_bytes) = inspect_container_body(&state, &id).await {
                let canonical = canonical_id_or_fallback(&id, &body_bytes);
                if let Some(name) = extract_container_name(&body_bytes) {
                    state
                        .runtime
                        .register_container_alias(&name, &canonical)
                        .await;
                }
                if let Some((aliases, ip)) = extract_container_dns_info(&body_bytes) {
                    state.runtime.register_dns(&canonical, &aliases, ip).await;
                }
            }
        }
    }

    Ok(response)
}

/// Remove a container and tear down its port forwarding + DNS.
///
/// # Errors
///
/// Returns an error if VM readiness fails or proxying to guest dockerd fails.
#[tracing::instrument(
    name = "docker.container.remove",
    skip(state, req),
    fields(uri = %uri, utility_vm = "native", container_id = tracing::field::Empty),
    err
)]
pub async fn remove_container(
    State(state): State<AppState>,
    OriginalUri(uri): OriginalUri,
    req: Request<Body>,
) -> Result<Response> {
    if let Some(id) = extract_container_id(&uri) {
        tracing::Span::current().record("container_id", id.as_str());
    }
    // Resolve the canonical ID from the host registry (no guest round-trip).
    let canonical = resolve_or_raw_for_teardown(&state, &uri).await;

    let response = proxy_to_system_vm(&state, &uri, req).await?;

    // Only tear down networking after a successful remove.
    if response.status().is_success() {
        if let Some(canonical) = canonical {
            state.runtime.stop_port_forwarding_by_id(&canonical).await;
            state.runtime.deregister_dns_by_id(&canonical).await;
        }
    }

    Ok(response)
}

/// Inspect a started container and configure port forwarding + DNS registration.
///
/// Shares a single inspect call for both port forwarding and DNS setup.
async fn setup_container_networking(state: &AppState, container_id: &str) {
    let Some(body_bytes) = inspect_container_body(state, container_id).await else {
        tracing::warn!(
            container_id,
            "Failed to inspect container for networking setup; \
             port forwarding and DNS will not be configured"
        );
        return;
    };

    // Use the canonical full container ID from inspect (not the URI token which
    // may be a name or short ID) so that stop/remove can reliably match the key.
    let canonical_id = canonical_id_or_fallback(container_id, &body_bytes);

    // Record the name → ID alias so later lifecycle calls (stop/kill/remove by
    // name or short ID) resolve without a guest round-trip.
    if let Some(name) = extract_container_name(&body_bytes) {
        state
            .runtime
            .register_container_alias(&name, &canonical_id)
            .await;
    }

    // Port forwarding.
    setup_port_forwarding_from_inspect(state, &canonical_id, &body_bytes).await;

    // DNS registration.
    if let Some((aliases, ip)) = extract_container_dns_info(&body_bytes) {
        state
            .runtime
            .register_dns(&canonical_id, &aliases, ip)
            .await;
    }
}

/// Fetches the inspect JSON body for a container from guest dockerd.
async fn inspect_container_body(state: &AppState, container_id: &str) -> Option<Bytes> {
    crate::guest_query::inspect_container(state.proxy.client(), container_id).await
}

/// Configures port forwarding from pre-fetched inspect JSON.
async fn setup_port_forwarding_from_inspect(
    state: &AppState,
    canonical_id: &str,
    body_bytes: &[u8],
) {
    let bindings = parse_port_bindings(body_bytes);
    if bindings.is_empty() {
        tracing::debug!("No port bindings found for container {}", canonical_id);
        return;
    }

    tracing::info!(
        "Port forwarding: {} bindings for container {}",
        bindings.len(),
        canonical_id,
    );
    for b in &bindings {
        tracing::info!(
            "  bind {}:{} → container:{}/{}",
            b.host_ip,
            b.host_port,
            b.container_port,
            b.protocol,
        );
    }

    let rules: Vec<_> = bindings
        .iter()
        .map(|b| {
            (
                b.host_ip.clone(),
                b.host_port,
                b.container_port,
                b.protocol.clone(),
            )
        })
        .collect();

    let machine_name = state.runtime.default_machine_name();
    if let Err(e) = state
        .runtime
        .start_port_forwarding_for(machine_name, canonical_id, &rules)
        .await
    {
        tracing::warn!(
            utility_vm = "native",
            "Failed to start port forwarding for {}: {}",
            canonical_id,
            e,
        );
    }
}

/// Extracts DNS aliases and IP address from Docker inspect JSON.
///
/// For compose containers (with `com.docker.compose.project` and
/// `com.docker.compose.service` labels), returns:
/// `["service.project", "container_name"]` — a hierarchical service alias
/// plus the flat container name.
///
/// For plain containers, returns `["container_name"]`.
///
/// Falls back through `/NetworkSettings/IPAddress` → per-network IPs.
pub fn extract_container_dns_info(inspect_json: &[u8]) -> Option<(Vec<String>, IpAddr)> {
    let v: serde_json::Value = serde_json::from_slice(inspect_json).ok()?;
    let name = v.get("Name")?.as_str()?.trim_start_matches('/').to_string();

    if name.is_empty() {
        return None;
    }

    // Primary: top-level IPAddress.
    let ip_str = v
        .pointer("/NetworkSettings/IPAddress")
        .and_then(|v| v.as_str())
        .filter(|s| !s.is_empty())
        // Fallback: first non-empty IP from Networks map.
        .or_else(|| {
            v.pointer("/NetworkSettings/Networks")?
                .as_object()?
                .values()
                .find_map(|net| net.get("IPAddress")?.as_str().filter(|s| !s.is_empty()))
        })?;

    // Build DNS aliases from compose labels when available.
    let aliases = match v.pointer("/Config/Labels").and_then(|l| l.as_object()) {
        Some(labels) => {
            let project = labels
                .get("com.docker.compose.project")
                .and_then(|v| v.as_str())
                .filter(|s| !s.is_empty());
            let service = labels
                .get("com.docker.compose.service")
                .and_then(|v| v.as_str())
                .filter(|s| !s.is_empty());
            match (project, service) {
                // Compose: service-level hierarchical + flat container name.
                (Some(proj), Some(svc)) => {
                    vec![format!("{svc}.{proj}"), name]
                }
                _ => vec![name],
            }
        }
        None => vec![name],
    };

    Some((aliases, ip_str.parse().ok()?))
}

/// Rename a container and update its DNS entry.
///
/// # Errors
///
/// Returns an error if VM readiness fails or proxying to guest dockerd fails.
#[tracing::instrument(
    name = "docker.container.rename",
    skip(state, req),
    fields(
        uri = %uri,
        utility_vm = "native",
        container_id = tracing::field::Empty,
        new_name = tracing::field::Empty,
    ),
    err
)]
pub async fn rename_container(
    State(state): State<AppState>,
    OriginalUri(uri): OriginalUri,
    req: Request<Body>,
) -> Result<Response> {
    if let Some(id) = extract_container_id(&uri) {
        tracing::Span::current().record("container_id", id.as_str());
    }
    // Resolve the canonical ID from the host registry BEFORE proxying — the
    // old name alias is dropped as part of the post-rename refresh.
    let canonical = resolve_canonical_from_uri(&state, &uri).await;
    let new_name = query_param(&uri, "name").map(str::to_string);
    if let Some(name) = new_name.as_deref() {
        tracing::Span::current().record("new_name", name);
    }

    // Proxy rename to guest.
    let response = proxy_to_system_vm(&state, &uri, req).await?;

    if response.status().is_success() {
        if let Some(ref canonical) = canonical {
            // Inspect FIRST (by canonical ID, which survives rename) and only
            // replace the old registration once usable fresh data is in hand.
            // Tearing down before a failed/unusable inspect would leave the
            // container with no DNS entry and no resolvable alias at all —
            // stale old-name DNS is strictly better than none.
            let refreshed = match inspect_container_body(&state, canonical).await {
                Some(body_bytes) => {
                    let name = extract_container_name(&body_bytes);
                    let dns = extract_container_dns_info(&body_bytes);
                    if name.is_none() && dns.is_none() {
                        false
                    } else {
                        state.runtime.deregister_dns_by_id(canonical).await;
                        if let Some(name) = name.as_deref() {
                            state
                                .runtime
                                .register_container_alias(name, canonical)
                                .await;
                        }
                        if let Some((aliases, ip)) = dns {
                            state.runtime.register_dns(canonical, &aliases, ip).await;
                        }
                        true
                    }
                }
                None => false,
            };
            if !refreshed {
                // Keep the old registration, but make sure the NEW name
                // (known from the query) resolves for later lifecycle calls —
                // otherwise stop/rm by the new name would be registry misses
                // and their teardown a no-op.
                if let Some(name) = new_name.as_deref() {
                    state
                        .runtime
                        .register_container_alias(name, canonical)
                        .await;
                }
                tracing::warn!(
                    container_id = %canonical,
                    "post-rename inspect unusable; keeping previous DNS registration"
                );
            }
        }
    }

    Ok(response)
}

/// Extracts the canonical full container ID from a Docker inspect JSON response.
fn extract_canonical_id_from_inspect(inspect_json: &[u8]) -> Option<String> {
    let value: serde_json::Value = serde_json::from_slice(inspect_json).ok()?;
    value.get("Id")?.as_str().map(String::from)
}

/// Returns the canonical container ID from inspect JSON, falling back to the
/// original request token when the inspect payload does not contain a usable ID.
pub(super) fn canonical_id_or_fallback(container_id: &str, inspect_json: &[u8]) -> String {
    extract_canonical_id_from_inspect(inspect_json).unwrap_or_else(|| container_id.to_string())
}

/// Extracts a container's unique name from Docker inspect JSON.
pub fn extract_container_name(inspect_json: &[u8]) -> Option<String> {
    let value: serde_json::Value = serde_json::from_slice(inspect_json).ok()?;
    let name = value.get("Name")?.as_str()?.trim_start_matches('/');
    (!name.is_empty()).then(|| name.to_string())
}

/// Extracts a container identifier from the URI and resolves it to the
/// canonical full ID of a container with registered host networking state.
///
/// Resolution is purely against the host registry (exact ID, name alias, or
/// unique short-ID prefix) — no guest round-trip. Every container that got
/// host networking was registered with its name at setup, so `None` means
/// the container has no host state to refresh or tear down.
async fn resolve_canonical_from_uri(state: &AppState, uri: &Uri) -> Option<String> {
    let token = extract_container_id(uri)?;
    state.runtime.resolve_registered_container(&token).await
}

/// Variant of [`resolve_canonical_from_uri`] for teardown handlers
/// (stop/kill/remove): when the token resolves to nothing registered, the
/// raw token is returned as-is. Teardown only matters for containers *with*
/// host state, so an unresolved token makes the teardown calls harmless
/// no-ops against the canonical-keyed maps; the reconciler backstops
/// anything the registry missed.
async fn resolve_or_raw_for_teardown(state: &AppState, uri: &Uri) -> Option<String> {
    let token = extract_container_id(uri)?;
    Some(
        state
            .runtime
            .resolve_registered_container(&token)
            .await
            .unwrap_or(token),
    )
}

#[cfg(test)]
mod tests;