|
| 1 | +--- |
| 2 | +name: rust-docker |
| 3 | +description: Build, check, run, or test this Rust workspace in Docker when Cargo is unavailable on the local node. Use for local Rust validation and reproductions without installing a host toolchain. |
| 4 | +license: AGPL-3.0 |
| 5 | +metadata: |
| 6 | + author: eisber |
| 7 | + version: "1.0" |
| 8 | +compatibility: Requires a running Docker daemon and PowerShell |
| 9 | +allowed-tools: Bash |
| 10 | +--- |
| 11 | + |
| 12 | +# Rust validation with Docker |
| 13 | + |
| 14 | +Run Rust commands locally in the pinned `rust:1.91-bookworm` image. This matches |
| 15 | +the workspace's `rust-version = "1.91"` requirement and avoids modifying the |
| 16 | +host toolchain. |
| 17 | + |
| 18 | +## Prepare once |
| 19 | + |
| 20 | +From the repository root in PowerShell: |
| 21 | + |
| 22 | +```powershell |
| 23 | +docker info |
| 24 | +docker pull rust:1.91-bookworm |
| 25 | +docker volume create lox-cargo-registry |
| 26 | +docker volume create lox-cargo-target |
| 27 | +``` |
| 28 | + |
| 29 | +The named volumes preserve downloaded dependencies and compiled artifacts |
| 30 | +between runs. |
| 31 | + |
| 32 | +## Run Cargo |
| 33 | + |
| 34 | +Resolve the repository path before mounting it: |
| 35 | + |
| 36 | +```powershell |
| 37 | +$repo = (Get-Location).Path |
| 38 | +docker run --rm ` |
| 39 | + --mount "type=bind,source=$repo,target=/work" ` |
| 40 | + --volume lox-cargo-registry:/usr/local/cargo/registry ` |
| 41 | + --volume lox-cargo-target:/work/target ` |
| 42 | + --workdir /work ` |
| 43 | + rust:1.91-bookworm ` |
| 44 | + cargo test --workspace --release |
| 45 | +``` |
| 46 | + |
| 47 | +Replace the final Cargo arguments as needed: |
| 48 | + |
| 49 | +```text |
| 50 | +cargo fetch --locked |
| 51 | +cargo check --workspace |
| 52 | +cargo build --workspace --release |
| 53 | +cargo test test_name -- --nocapture |
| 54 | +cargo run -- <lox arguments> |
| 55 | +``` |
| 56 | + |
| 57 | +Pass Cargo directly as the container command. Do not wrap it in `sh -lc` or |
| 58 | +`bash -lc`, because a login shell can replace the image's Rust `PATH`. |
| 59 | + |
| 60 | +## Run against a temporary base worktree |
| 61 | + |
| 62 | +For before/after regressions, create a detached worktree under the repository |
| 63 | +and mount it independently: |
| 64 | + |
| 65 | +```powershell |
| 66 | +git worktree add --detach .tmp-base origin/main |
| 67 | +$base = (Resolve-Path .tmp-base).Path |
| 68 | +docker run --rm ` |
| 69 | + --mount "type=bind,source=$base,target=/work" ` |
| 70 | + --volume lox-cargo-registry:/usr/local/cargo/registry ` |
| 71 | + --volume lox-cargo-target:/work/target ` |
| 72 | + --workdir /work ` |
| 73 | + rust:1.91-bookworm ` |
| 74 | + cargo test test_name -- --nocapture |
| 75 | +git worktree remove .tmp-base |
| 76 | +``` |
| 77 | + |
| 78 | +Use a different target volume when base and head build artifacts must remain |
| 79 | +strictly isolated. |
| 80 | + |
| 81 | +## Limitations |
| 82 | + |
| 83 | +The pinned official image includes `cargo` and `rustc`, but not `rustfmt`, |
| 84 | +Clippy, or `rustup`. Use the repository CI or an image explicitly built with |
| 85 | +those components for `cargo fmt` and `cargo clippy`; do not report them as |
| 86 | +validated from this image. |
0 commit comments