diff --git a/CHANGELOG.md b/CHANGELOG.md index 1130f46..85efbc0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,14 @@ # 更新日志 +## 2026-10-26 → ✨ 本地镜像显示使用状态,运行中镜像不可删除 + +- ✨ 镜像表格新增「使用情况」列:`运行中 N 个容器`(红)/ `已停止容器引用 N`(黄)/ `未使用`(灰),悬停显示具体容器名 +- ✨ **被运行中容器使用的镜像不再显示删除按钮**,改为灰字「使用中,不可删除」 +- ✨ 底部统计:共 N 个 · 合计 X · 使用中 N 个 · 可删除 N 个(可释放 X) +- 🔧 后端 `GET /api/images` 返回 `running` / `stopped` / `inUse` / `canDelete` 与 `summary` +- 🔧 `DELETE /api/images/:id` 加服务端兜底:被运行中容器使用返回 `409`「该镜像正被运行中的容器使用(容器名),不能删除」;被已停止容器引用提示先删容器(防止前端绕过) +- ✅ 实测:181 个镜像 / 合计 73.1GB / 使用中 59 个 / 可删除 122 个(33.8GB);直接调接口删使用中镜像返回 409 并被拒绝 + ## 2026-10-26 → ✨ 停止的容器支持批量删除 - ✨ 停止的容器页每行加复选框 + 顶部批量条:全选、已选数量、删除选中(按钮显示「删除选中 N 个」) diff --git a/public/app.js b/public/app.js index 4f42810..5e26132 100644 --- a/public/app.js +++ b/public/app.js @@ -850,14 +850,29 @@ function drawChart(root, which, values, fmt, color) { /* ---------------- 镜像 / 仓库 / 网络 / 设置 ---------------- */ async function renderImages() { - const imgs = await api('/api/images'); - const total = imgs.reduce((a, b) => a + b.size, 0); - $('#view').innerHTML = ` - ${imgs.map(i => ` - + const data = await api('/api/images'); + const imgs = data.items || []; + const s = data.summary || { total: imgs.length, totalSize: 0, inUse: 0, deletable: 0, deletableSize: 0 }; + const useCell = (i) => { + if (i.running && i.running.length) { + return `运行中 ${i.running.length} 个容器`; + } + if (i.stopped && i.stopped.length) { + return `已停止容器引用 ${i.stopped.length}`; + } + return '未使用'; + }; + $('#view').innerHTML = `
镜像ID大小创建
${(i.tags.length ? i.tags : [':']).map(t => `${esc(t)}`).join(' ')}${esc(i.shortId)}${fmtBytes(i.size)}
+ ${imgs.map(i => ` + + + + - `).join('')} -
镜像ID大小使用情况创建
${(i.tags.length ? i.tags : [':']).map(t => `${esc(t)}`).join(' ')}${esc(i.shortId)}${fmtBytes(i.size)}${useCell(i)} ${new Date(i.created * 1000).toLocaleDateString('zh-CN')}
共 ${imgs.length} 个镜像 · 合计 ${fmtBytes(total)}
`; + ${i.canDelete ? `` : '使用中,不可删除'} + `).join('')} + +
共 ${s.total} 个镜像 · 合计 ${fmtBytes(s.totalSize)} · 使用中 ${s.inUse} 个 · 可删除 ${s.deletable} 个(可释放 ${fmtBytes(s.deletableSize)})
`; $$('#view [data-img]').forEach(b => b.onclick = async () => { if (!confirm('确定删除该镜像?')) return; try { await api(`/api/images/${encodeURIComponent(b.dataset.img)}?force=1`, { method: 'DELETE' }); toast('已删除'); render(); } diff --git a/server/index.js b/server/index.js index a40fb66..dc5e08e 100644 --- a/server/index.js +++ b/server/index.js @@ -1024,17 +1024,52 @@ app.post('/api/containers', async (req, res) => { /* ---------------- 接口:镜像 / 网络 ---------------- */ app.get('/api/images', async (req, res) => { try { - const imgs = await docker.listImages({ all: false }); - res.json(imgs.map(i => ({ - id: i.Id, shortId: i.Id.replace('sha256:', '').slice(0, 12), tags: i.RepoTags || [], - size: i.Size, created: i.Created, containers: i.Containers || 0, - })).sort((a, b) => b.size - a.size)); + const [imgs, conts] = await Promise.all([ + docker.listImages({ all: false }), + docker.listContainers({ all: true }), + ]); + const use = new Map(); + for (const c of conts) { + const k = c.ImageID; + if (!use.has(k)) use.set(k, { running: [], stopped: [] }); + const b = use.get(k); + (c.State === 'running' ? b.running : b.stopped).push(strip((c.Names || [])[0])); + } + const list = imgs.map(i => { + const u = use.get(i.Id) || { running: [], stopped: [] }; + return { + id: i.Id, shortId: i.Id.replace('sha256:', '').slice(0, 12), tags: i.RepoTags || [], + size: i.Size, created: i.Created, containers: i.Containers || 0, + running: u.running, stopped: u.stopped, + inUse: u.running.length > 0, + canDelete: u.running.length === 0 && u.stopped.length === 0, + }; + }).sort((a, b) => b.size - a.size); + res.json({ + items: list, + summary: { + total: list.length, + totalSize: list.reduce((a, b) => a + b.size, 0), + inUse: list.filter(i => i.inUse).length, + deletable: list.filter(i => i.canDelete).length, + deletableSize: list.filter(i => i.canDelete).reduce((a, b) => a + b.size, 0), + }, + }); } catch (e) { res.status(500).json({ error: e.message }); } }); app.delete('/api/images/:id', async (req, res) => { - try { await docker.getImage(req.params.id).remove({ force: req.query.force === '1' }); res.json({ ok: true }); } - catch (e) { res.status(500).json({ error: e.message }); } + try { + const id = req.params.id; + const img = await docker.getImage(id).inspect(); + const conts = await docker.listContainers({ all: true }); + const running = conts.filter(c => c.ImageID === img.Id && c.State === 'running').map(c => strip((c.Names || [])[0])); + if (running.length) return res.status(409).json({ error: `该镜像正被运行中的容器使用(${running.slice(0, 3).join('、')}${running.length > 3 ? ' 等' : ''}),不能删除` }); + const stopped = conts.filter(c => c.ImageID === img.Id).map(c => strip((c.Names || [])[0])); + if (stopped.length) return res.status(409).json({ error: `该镜像被已停止的容器引用(${stopped.slice(0, 3).join('、')}),请先删除容器` }); + await docker.getImage(id).remove({ force: req.query.force === '1' }); + res.json({ ok: true }); + } catch (e) { res.status(500).json({ error: e.message }); } }); app.post('/api/images/prune', async (req, res) => {