// Docker: containers grouped by the stack that deployed them, with start / stop / restart, // logs that follow, and inspect — then images, volumes and networks. Live CPU and memory // come from the same container sensors everything else uses. import { FormEvent, useCallback, useEffect, useMemo, useRef, useState } from 'react' import { api, DockerContainer, DockerEvent, DockerHealth, DockerInfo, DockerLogLine, ImageCheck, MachineUpdates, Stack, StackDiff, UpdateJob, UpdateJobRow, wsURLPath } from './api' import TaskOutput from './TaskOutput' import StackNew, { DupFrom } from './StackNew' import { Sensor, fetchHistories, fmtValue, useSensorStream } from './sensors' import { useHashSel } from './hashsel' import { fmtWindowSec, useHistorySec, useRefreshMs } from './prefs' import Sparkline from './Sparkline' import { fmtBytes } from './fmt' import { describeCron } from './crontext' import { askText, askYes, Btn, CloseBtn, Loading, setUnsaved, toast, useEscape } from './ui' type Tab = 'containers' | 'stacks' | 'updates' | 'images' | 'volumes' | 'networks' const dot = (c: DockerContainer) => (c.state !== 'running' ? (c.state === 'exited' || c.state === 'dead' ? 'bad' : 'warn') : c.health === 'unhealthy' ? 'bad' : c.health === 'starting' ? 'warn' : 'ok') const ports = (p: string) => [...new Set(p.split(', ').map((x) => x.replace(/^\[?::\]?:/, '').replace(/^0\.0\.0\.0:/, '').replace(/^[\d.]+:/, '')).filter((x) => x.includes('->')))].join(' ') // A container's output, followed over a held connection the way the Services tab follows a // journal. A reconnect resumes from the last line's timestamp, so a container that restarts // — or a phone that slept — doesn't replay what is already on screen. function LogView({ id }: { id: string }) { const [lines, setLines] = useState([]) const [err, setErr] = useState('') const [live, setLive] = useState(false) const [follow, setFollow] = useState(true) const [paused, setPaused] = useState(false) const box = useRef(null) const last = useRef('') const pausedRef = useRef(false) pausedRef.current = paused const pending = useRef([]) // What a resume has already shown: `--since` is inclusive, and two lines can share a // timestamp (a container writing to both streams at once), so the boundary is a set of // lines rather than a moment. Bounded — only the lines around a reconnect can repeat. const seen = useRef<{ set: Set; q: string[] }>({ set: new Set(), q: [] }) useEffect(() => { setLines([]); setErr(''); setLive(false); last.current = ''; pending.current = [] seen.current = { set: new Set(), q: [] } let closed = false let sock: WebSocket | null = null let retry: number | undefined let attempt = 0 const connect = () => { if (closed) return const q = last.current ? `since=${encodeURIComponent(last.current)}` : 'n=300' // fixed when the log opens: a reconnect must not follow the console to another machine const s = new WebSocket(wsURLPath(`/api/docker/containers/${encodeURIComponent(id)}/logs/tail?${q}`)) sock = s s.onopen = () => { attempt = 0; setLive(true); setErr('') } s.onmessage = (ev) => { const l = JSON.parse(ev.data) as DockerLogLine if (l.t) { const k = l.t + '|' + (l.err ? 'e' : 'o') + '|' + l.m const { set, q } = seen.current if (set.has(k)) return set.add(k); q.push(k) if (q.length > 1000) for (const old of q.splice(0, 500)) set.delete(old) if (l.t > last.current) last.current = l.t } if (pausedRef.current) { pending.current.push(l); return } setLines((prev) => (prev.length > 2000 ? [...prev.slice(-1500), l] : [...prev, l])) } // a container that has stopped — or was never there — ends the stream at once, so // back off rather than asking Docker the same question twice a second forever s.onclose = () => { if (closed) return; setLive(false); retry = window.setTimeout(connect, Math.min(15000, 1000 * 2 ** attempt++)) } s.onerror = () => { setErr('log stream disconnected'); s.close() } } connect() return () => { closed = true; clearTimeout(retry); sock?.close() } }, [id]) // Resume: flush what arrived while paused. useEffect(() => { if (paused || pending.current.length === 0) return const p = pending.current; pending.current = [] setLines((prev) => [...prev, ...p].slice(-2000)) }, [paused]) useEffect(() => { if (follow && !paused && box.current) box.current.scrollTop = box.current.scrollHeight }, [lines, follow, paused]) return (
{live ? 'following' : 'reconnecting…'} · {lines.length} lines{err && ` · ${err}`}
{ const el = e.currentTarget; if (el.scrollHeight - el.scrollTop - el.clientHeight > 40) setFollow(false) }}> {lines.map((l, i) =>
{l.t ? new Date(l.t).toLocaleTimeString() : ''}{l.m}
)} {lines.length === 0 && !err &&
No output yet.
}
) } // What a container is doing, and what can be done to it: its numbers over time, its health // checks, what has happened to it lately, a shell inside it, and its limits. function Detail({ c, live, onBack, onChanged }: { c: DockerContainer; live: Record; onBack: () => void; onChanged: () => void }) { const [tab, setTab] = useState<'logs' | 'stats' | 'health' | 'events' | 'inspect'>('logs') const [info, setInfo] = useState | null>(null) const [limits, setLimits] = useState(false) useEffect(() => { setInfo(null); if (tab === 'inspect' || limits) api.dockerInspect(c.name).then(setInfo).catch((e) => toast(e instanceof Error ? e.message : 'failed', 'err')) }, [tab, limits, c.name]) const act = (a: 'start' | 'stop' | 'restart' | 'remove') => async () => { try { await api.dockerAction(c.name, a); toast(`${c.name}: ${a} ok`); onChanged() } catch (e) { toast(`${c.name}: ${e instanceof Error ? e.message : 'failed'}`, 'err') } } const running = c.state === 'running' const seg = c.name.toLowerCase().replace(/[^a-z0-9-]+/g, '_').replace(/^_|_$/g, '') const s = (k: string) => live[`container.${seg}.${k}`] const mounts = (info?.mounts as { Source: string; Destination: string; RW: boolean }[] | undefined) ?? [] const shell = async () => { try { const t = await api.dockerExec(c.name); toast(`shell open as “${t.name}” on the Terminal tab`); window.dispatchEvent(new CustomEvent('hamlab:terminal', { detail: t.id })) } catch (e) { toast(e instanceof Error ? e.message : 'failed', 'err') } } return (
{c.name}{c.health && c.health !== 'none' && {c.health}}
Image{c.image}
Status{c.status}
{c.stack &&
Stack{c.stack}{c.service && c.service !== c.name ? ` · ${c.service}` : ''}
} {running &&
Using{fmtValue(s('cpu')?.value, '%')} of a core · {fmtValue(s('mem')?.value ?? c.mem, 'B')}
} {ports(c.ports) &&
Ports{ports(c.ports)}
}
{!running && Start} {!running && { await act('remove')(); onBack() }}>Remove} {running && Shell} {running && Restart} setLimits(true)}>Limits {running && Stop}
{tab === 'logs' && } {tab === 'stats' && } {tab === 'health' && } {tab === 'events' && } {tab === 'inspect' && (!info ? : (
Restarts{String(info.restart || 'no')} policy · restarted {String(info.restarts ?? 0)}×
Network{Object.keys((info.networks as object) ?? {}).join(', ') || String(info.network_mode)}
{Number(info.memory_limit) > 0 &&
Mem limit{fmtValue(Number(info.memory_limit), 'B')}
} {!!info.error &&
Error{String(info.error)}
} {!!info.compose_file &&
Compose{String(info.compose_file)}
} {mounts.length > 0 &&
Mounts{mounts.map((m) =>
{m.Source} → {m.Destination}{m.RW ? '' : ' (read only)'}
)}
}
Env names only{((info.env_names as string[]) ?? []).join(' ') || '—'}
))} {limits && setLimits(false)} onSaved={() => { setLimits(false); onChanged() }} />}
) } // CPU and memory over time, from the container sensors the sampler already keeps. function Stats({ seg, live }: { seg: string; live: Record }) { const [hist, setHist] = useState<{ id: string; t: number[]; v: number[] }[] | null>(null) const historySec = useHistorySec() useEffect(() => { let alive = true fetchHistories(`container.${seg}.cpu,container.${seg}.mem`) .then((h) => { if (alive) setHist(h) }) .catch(() => { if (alive) setHist([]) }) return () => { alive = false } }, [seg]) const css = (n: string) => getComputedStyle(document.documentElement).getPropertyValue(n).trim() || '#3987e5' const of = (k: string) => hist?.find((h) => h.id === `container.${seg}.${k}`)?.v ?? [] const cpu = of('cpu'), mem = of('mem') if (hist === null) return if (cpu.length < 2 && mem.length < 2) return
No history yet for this container — it is sampled while it runs, so give it a moment.
return (
CPU{fmtValue(live[`container.${seg}.cpu`]?.value, '%')} of a core
`${v.toFixed(1)}%`} height={64} />
the last {fmtWindowSec(historySec)}
Memory{fmtValue(live[`container.${seg}.mem`]?.value, 'B')}
fmtBytes(v)} height={64} />
the last {fmtWindowSec(historySec)}
) } // The health check's verdict, and what its last few probes actually said. function Health({ id }: { id: string }) { const [h, setH] = useState(null) const [err, setErr] = useState('') useEffect(() => { setH(null); api.dockerHealth(id).then(setH).catch((e) => setErr(e instanceof Error ? e.message : 'failed')) }, [id]) if (err) return
{err}
if (!h) return if (!h.configured) return
This image defines no health check, so Docker only knows whether the process is running — which it is ({h.state}). A health check is a healthcheck: block in the compose file.
return ( <>
Health{h.status}
State{h.state}
{!!h.failing_streak &&
Failing{h.failing_streak} probe{h.failing_streak === 1 ? '' : 's'} in a row
}
The last {h.log?.length ?? 0} probes — Docker keeps five.
{(h.log ?? []).slice().reverse().map((p, i) => (
{new Date(p.Start).toLocaleString()} exit {p.ExitCode} · {Math.max(0, new Date(p.End).getTime() - new Date(p.Start).getTime())} ms
{p.Output.trim() &&
{p.Output.trim()}
}
))} ) } // What has happened to this container lately, asked of Docker rather than kept here. function Events({ id }: { id: string }) { const [ev, setEv] = useState(null) const [kept, setKept] = useState(0) const [since, setSince] = useState('7d') const [err, setErr] = useState('') useEffect(() => { setEv(null); setErr(''); api.dockerEvents(id, since).then((r) => { setEv(r.events); setKept(r.kept) }).catch((e) => setErr(e instanceof Error ? e.message : 'failed')) }, [id, since]) const bad = (a: string, h?: string) => /die|kill|oom|destroy/.test(a) || (h !== undefined && h !== 'healthy') return ( <>
{['24h', '7d', '30d'].map((x) => )}
{err &&
{err}
} {!ev && !err && } {ev && ev.length === 0 &&
Nothing for this container in the last {since}. Ham Lab keeps its own record — Docker's own only reaches back a few minutes — so history starts from when this version was installed. {kept} events kept across every container.
} {ev && ev.length > 0 && (
{ev.map((e, i) => (
{e.action}{e.health ? `: ${e.health}` : ''} {new Date(e.t).toLocaleString()} {e.exit_code ? `exit ${e.exit_code}` : ''}{e.signal ? ` signal ${e.signal}` : ''}
))}
)} ) } // Memory, CPU and restart policy, changed on the running container. Docker does this live, // which is the point — and also the catch, said plainly below. function LimitsDialog({ c, info, onClose, onSaved }: { c: DockerContainer; info: Record | null; onClose: () => void; onSaved: () => void }) { const curMem = Number(info?.memory_limit ?? 0) const [mem, setMem] = useState(curMem > 0 ? String(Math.round(curMem / (1 << 20))) + 'm' : '') const [cpus, setCpus] = useState('') const [restart, setRestart] = useState(String(info?.restart ?? '')) const [err, setErr] = useState('') const [busy, setBusy] = useState(false) useEscape(onClose) const save = async (e: FormEvent) => { e.preventDefault(); setErr(''); setBusy(true) try { await api.dockerLimits(c.name, { memory: mem, cpus, restart }); toast(`${c.name} updated`); onSaved() } catch (e) { setErr(e instanceof Error ? e.message : 'failed'); setBusy(false) } } return (
e.stopPropagation()} onSubmit={save}>

Limits for {c.name}

This changes the running container now, without recreating it. It does not edit the compose file{c.stack ? <> — so deploying {c.stack} puts back whatever the file says : ''}. Edit the stack to make it stick.
{err &&
{err}
}
) } function lineDiff(a: string, b: string): { s: ' ' | '+' | '-'; t: string }[] { const x = a.split('\n'), y = b.split('\n'); const n = x.length, m = y.length const L = Array.from({ length: n + 1 }, () => new Uint16Array(m + 1)) for (let i = n - 1; i >= 0; i--) for (let j = m - 1; j >= 0; j--) L[i][j] = x[i] === y[j] ? L[i + 1][j + 1] + 1 : Math.max(L[i + 1][j], L[i][j + 1]) const out: { s: ' ' | '+' | '-'; t: string }[] = []; let i = 0, j = 0 while (i < n && j < m) { if (x[i] === y[j]) { out.push({ s: ' ', t: x[i] }); i++; j++ } else if (L[i + 1][j] >= L[i][j + 1]) out.push({ s: '-', t: x[i++] }); else out.push({ s: '+', t: y[j++] }) } while (i < n) out.push({ s: '-', t: x[i++] }); while (j < m) out.push({ s: '+', t: y[j++] }) return out } // What a Deploy would change: each file against what this console last deployed (or, before // the first deploy from here, the copy the last save replaced). Only changed lines and a // little around them; a .env shows which keys changed, never their values. function DiffView({ files }: { files: StackDiff[] }) { const changed = files.filter((f) => f.base && f.old !== f.new) if (files.every((f) => !f.base)) return
Nothing to compare with yet: this console hasn't deployed or saved this stack before.
if (changed.length === 0) return
No changes since {files.some((f) => f.base === 'deployed') ? 'the last deploy from here' : 'the last save'}: Deploy would only recreate what Docker itself thinks has changed.
return <>{changed.map((f) => { const d = lineDiff(f.old, f.new); const keep = new Set(); d.forEach((l, i) => { if (l.s !== ' ') for (let k = i - 2; k <= i + 2; k++) keep.add(k) }) return
{f.name} · against {f.base === 'deployed' ? 'what was last deployed from here' : 'the copy the last save replaced'}
{d.map((l, i) => (keep.has(i) ? 
{l.s} {l.t}
: keep.has(i - 1) ?
⋯
: null))}
})} } // A .env is mostly secrets: keys shown, values hidden until asked for. const maskEnv = (text: string) => text.split('\n').map((l) => { const i = l.indexOf('='); return i > 0 && !l.trim().startsWith('#') && l.length > i + 1 ? l.slice(0, i + 1) + '••••••••' : l }).join('\n') // One stack: its compose file to edit, and the four things to do with it. Saving validates // with compose itself and keeps the previous file beside it; deploying is a separate, asked-for act. function StackDetail({ name, startTask, onBack, onChanged, onDuplicate }: { name: string; startTask?: string; onBack: () => void; onChanged: () => void; onDuplicate: (from: DupFrom) => void }) { const [st, setSt] = useState(null) const [text, setText] = useState(''); const [orig, setOrig] = useState('') const [err, setErr] = useState(''); const [task, setTask] = useState('') const [file, setFile] = useState(0); const [sum, setSum] = useState('') const [reveal, setReveal] = useState(false); const [diff, setDiff] = useState(null) const [upd, setUpd] = useState<{ images: ImageCheck[]; checked: number } | null>(null); const [checking, setChecking] = useState(false) useEffect(() => { setUpd(null); setDiff(null); api.stackUpdates(name).then(setUpd).catch(() => {}) }, [name]) useEffect(() => { setReveal(false) }, [file, name]) const load = useCallback(async () => { try { const r = await api.stackFile(name, file); setSt(r.stack); setText(r.text); setOrig(r.text); setSum(r.sum); setErr('') } catch (e) { setErr(e instanceof Error ? e.message : 'failed') } }, [name, file]) useEffect(() => { setSt(null); setTask(startTask ?? ''); load() }, [load]) // eslint-disable-line react-hooks/exhaustive-deps const dirty = text !== orig useEffect(() => { setUnsaved(dirty ? `${name}'s compose file` : ''); return () => setUnsaved('') }, [dirty, name]) const save = async () => { try { await api.saveStack(name, file, text, sum); setOrig(text); setErr(''); setDiff(null); api.stackFile(name, file).then((r) => setSum(r.sum)).catch(() => {}); toast('saved — the running containers are unchanged until you deploy') } catch (e) { setErr(e instanceof Error ? e.message : 'failed') } } const remove = async () => { const typed = await askText(`Delete the stack "${name}"?\n\nIts containers are removed and its compose file${st?.has_env ? ' and .env' : ''} moved to .hamlab-trash beside it — moved, not erased. Volumes and bind-mounted data are not touched.\n\nType its name to confirm.`, '', 'Delete') if (typed !== name) { if (typed !== null) toast('that is not its name — nothing was deleted', 'info'); return } try { const r = await api.deleteStack(name); toast(`${name} deleted — its files are in ${r.trash}`); setUnsaved(''); onChanged(); onBack() } catch (e) { toast(e instanceof Error ? e.message : 'failed', 'err') } } const duplicate = async () => { if (!st) return; try { onDuplicate({ name, text: file === 0 ? orig : (await api.stackFile(name, 0)).text, hasEnv: !!st.has_env, files: st.host_files?.length ?? 1 }) } catch (e) { toast(e instanceof Error ? e.message : 'failed', 'err') } } const run = (a: 'deploy' | 'update' | 'recreate' | 'restart' | 'stop' | 'down') => async () => { try { setTask((await api.stackAction(name, a)).id) } catch (e) { toast(e instanceof Error ? e.message : 'failed', 'err') } } if (err && !st) return
{err}
if (!st) return
return (
{st.name}{st.running}/{st.containers.length} running
{file === 'env' ? (st.host_file.replace(/[^/]+$/, '') + '.env') : (st.host_files?.[file] ?? st.host_file)}{st.held_by && · kept on {st.held_by}, deployed to this machine over {st.remote}}{st.via && · compose runs inside {st.via}, as it was deployed}{st.has_env && ' · has a .env beside it'}
{((st.host_files?.length ?? 0) > 1 || st.has_env) &&
{st.host_files!.map((f, i) => )}{st.has_env && }
} {file === 'env' && !reveal ?
{maskEnv(text)}
: