feat: 本地镜像显示使用状态,运行中镜像不可删除

This commit is contained in:
jinghui committed 2026-10-26 21:42:41 +08:00
1 parent f9701c92d8
commit f8ed288b5e
3 files changed
+73 -14

No files matched your search

+9
View File
@@ -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 个」)
+22 -7
View File
@@ -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 = `<table class="tb"><thead><tr><th>镜像</th><th>ID</th><th class="num">大小</th><th>创建</th><th></th></tr></thead><tbody>
${imgs.map(i => `<tr><td>${(i.tags.length ? i.tags : ['<none>:<none>']).map(t => `<span class="tag blue">${esc(t)}</span>`).join(' ')}</td>
<td class="muted">${esc(i.shortId)}</td><td class="num">${fmtBytes(i.size)}</td>
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 `<span class="pill bad" title="${esc(i.running.join('、'))}">运行中 ${i.running.length} 个容器</span>`;
}
if (i.stopped && i.stopped.length) {
return `<span class="pill warn" title="${esc(i.stopped.join('、'))}">已停止容器引用 ${i.stopped.length}</span>`;
}
return '<span class="pill stop">未使用</span>';
};
$('#view').innerHTML = `<table class="tb"><thead><tr><th>镜像</th><th>ID</th><th class="num">大小</th><th>使用情况</th><th>创建</th><th></th></tr></thead><tbody>
${imgs.map(i => `<tr>
<td>${(i.tags.length ? i.tags : ['<none>:<none>']).map(t => `<span class="tag blue">${esc(t)}</span>`).join(' ')}</td>
<td class="muted">${esc(i.shortId)}</td>
<td class="num">${fmtBytes(i.size)}</td>
<td>${useCell(i)}</td>
<td class="muted">${new Date(i.created * 1000).toLocaleDateString('zh-CN')}</td>
<td><button class="btn danger" data-img="${esc(i.id)}">删除</button></td></tr>`).join('')}
</tbody></table><div class="foot">共 ${imgs.length} 个镜像 · 合计 ${fmtBytes(total)}</div>`;
<td>${i.canDelete ? `<button class="btn danger" data-img="${esc(i.id)}">删除</button>` : '<span class="muted" style="font-size:11.5px">使用中,不可删除</span>'}</td>
</tr>`).join('')}
</tbody></table>
<div class="foot">共 ${s.total} 个镜像 · 合计 ${fmtBytes(s.totalSize)} · 使用中 ${s.inUse} 个 · 可删除 ${s.deletable} 个(可释放 ${fmtBytes(s.deletableSize)})</div>`;
$$('#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(); }
+42 -7
View File
@@ -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) => {