Architecture notes
Model-View-Update in Rust: one model, a pure ratatui view, a message-driven update function, and a Docker actor that keeps every blocking call off the render loop.
Stevedore is roughly 1,800 lines of Rust across four files. The split between those files is the whole design.
src/
├── app.rs # Model + Update: App, Message, all state transitions
├── ui.rs # View: pure rendering of the model with ratatui
├── docker.rs # Side effects: background actor owning the bollard client
└── main.rs # Terminal setup and the select! event loop
Model-View-Update is what holds those pieces apart. It's the pattern behind Elm and most serious Rust TUIs. One struct holds all state, one function renders it, one function changes it, and nothing else is allowed to do either.
The companion post covers why the pattern suits Rust in general. These notes cover how Stevedore wires it up.
The event loop
main.rs is the entire control flow, and it's short enough to read in one sitting.
Two unbounded channels carry the traffic. Messages flow into the update function, and Docker commands flow out to the actor.
let (msg_tx, mut msg_rx) = mpsc::unbounded_channel::<Message>();
let (cmd_tx, cmd_rx) = mpsc::unbounded_channel::<docker::DockerCommand>();
docker::spawn(msg_tx, cmd_rx);
let mut app = App::new(cmd_tx);
let mut input = EventStream::new();
loop {
terminal.draw(|frame| ui::draw(frame, &mut app))?;
tokio::select! {
maybe_event = input.next() => { /* key press -> Message::Key */ }
maybe_msg = msg_rx.recv() => { /* actor result -> app.update(msg) */ }
}
if app.should_quit { break }
}
The select! macro is what makes the two input sources equal citizens. A keystroke from crossterm and a container list refresh from the Docker poller race each other. Whichever arrives first is folded into the model, and the frame is redrawn.
Neither source can starve or block the other. That's why the list keeps refreshing while you hold down a navigation key, and why holding that key doesn't delay a refresh.
Redrawing before every wait is safe because ratatui is immediate mode. The frame is rebuilt from the model on each pass, and only the difference is written to the terminal.
The Model
App in app.rs holds everything a frame needs. Nothing else in the program holds UI state.
pub struct App {
pub containers: Vec<ContainerInfo>,
pub list_state: ListState,
pub view: View, // Details | Logs | Update
pub logs: Vec<String>,
pub stats: Option<StatsInfo>,
pub status: Option<StatusMessage>,
pub scroll_back: usize,
pub update_output: Vec<String>,
pub update_running: bool,
pub should_quit: bool,
cmd_tx: UnboundedSender<DockerCommand>,
// ...
}
Two of those fields deserve a note.
scroll_back is a distance from the newest line, not an absolute offset. Zero means follow mode.
Storing it that way means an arriving log line never has to adjust it. The behaviour of staying pinned to the bottom falls out of the representation itself, rather than being maintained by hand. The view clamps the offset against the real buffer length when it renders.
cmd_tx is the model's one concession to the outside world. Update is synchronous and must not block, so instead of performing work it requests work by sending a command down this channel. That's Stevedore's version of Elm's rule that update returns a command.
The Update
Every event in the program is a variant of one enum. That enum doubles as the program's specification.
pub enum Message {
Key(KeyEvent),
ContainersUpdated(Vec<ContainerInfo>),
StatsUpdated(String, StatsInfo),
LogLine(String, String),
UpdateLine(String),
UpdateFinished { success: bool, detail: String },
Status(String),
DockerError(String),
}
App::update folds one message into the model and returns. Because every mutation lands in that single match, invariants that would otherwise be scattered across components all stay in one place.
Stale results are dropped rather than applied. Stats and log messages carry the container ID they belong to, and they are ignored unless that container is still selected. An async reply for a container you have already scrolled past can't corrupt the pane.
Buffers stay bounded. Log lines and update output drain from the front past 2,000 lines, so a chatty container can't grow the process without limit.
Selection survives refreshes. A container list update re-finds the previously selected container by its ID, and only falls back to clamping the index when that container is gone.
That last one prevents a classic panic. A refresh shrinks the list while the cursor sits near the bottom, and the index goes out of bounds. It can't happen here, because the list and the cursor are re-established together in one arm of one function.
Key handling is a nested match inside the key variant. That keeps the terminal-specific key code vocabulary from leaking into the rest of the update logic.
The View
ui::draw reads the model and paints it.
The layout is a vertical split into a one line header, the body, and a one line footer. The body is then split horizontally, 35 to 65, into the container list and the active right pane.
The view is read-only in every sense that matters, with one deliberate exception. It takes a mutable reference because ratatui's list widget owns the scroll offset it computes while rendering. That's state about the widget rather than about the application, and it's the only thing the view is allowed to touch.
Everything else is derivation. The footer, for instance, is not stored anywhere. It's a match on the current view, so the hints on screen can't drift out of sync with the keys that view actually accepts.
Side effects: the Docker actor
docker.rs owns the bollard client. Nothing else in the program has one.
The actor is spawned once, receives commands, and reports back as messages.
pub enum DockerCommand {
Start { id: String, name: String },
Stop { id: String, name: String },
Restart { id: String, name: String },
FetchStats { id: String },
StreamLogs { id: String },
StopLogs,
ComposeUpdate { compose: ComposeInfo, name: String },
}
Its command loop dispatches work without ever awaiting it. Each command spawns its own task, so a hung Docker call can't stall the next command, let alone the UI. A separate ticker task polls the container list every two seconds and pushes an update on its own schedule.
Log streaming gets one extra rule. The actor keeps a handle for the active stream, and a new stream request aborts the previous one before starting. Exactly one stream is alive at a time, so scrolling quickly through twenty containers doesn't leave twenty tasks writing into the same buffer.
Compose updates as a subprocess
The update feature is the one place where Stevedore shells out.
The reason is honest. bollard speaks to the Docker Engine API, but Compose is a CLI plugin with no equivalent at the daemon level. So the implementation runs the real command.
docker compose -p <project> -f <config...> pull <service>
docker compose -p <project> -f <config...> up -d --build --no-deps <service>
The two steps are not equally important, and the type says so. Each carries a required flag, and only the second one is required.
A service built from source has an image: tag that exists in no registry, so pull fails for it every time with "pull access denied". Treating that as fatal meant locally built services could never be updated at all, which is most of a developer's own stacks. The pull is now best effort, and --build on the up step is what rebuilds those services.
The project name, the config files, and the working directory all come from the container's own Compose labels, rather than from the shell that launched Stevedore. The update therefore targets exactly the stack that created that container.
Standard output and standard error are piped and forwarded line by line as update messages, which is why the output appears live rather than arriving in one dump at the end. The child process is spawned so that it's killed on drop, so quitting Stevedore doesn't orphan it. On success, the actor pushes a fresh container list immediately instead of waiting for the next tick.
What the architecture buys
The first payoff is diagnostic. Every state change passes through one function, so if the screen is wrong then either the model is wrong or the view misread it. One debug print at the top of update tells you which. There is no third place to look.
The second payoff is that the borrow checker stays quiet. Rendering takes an immutable view of the model and updating takes a mutable one, and the two phases never overlap in time. The aliasing question that makes object graph UIs painful in Rust simply never comes up, and no reference counted interior mutability appears anywhere in the codebase.