From f8ed288b5e08f350fdb3afa6f4dcab945c1b3bc9 Mon Sep 17 00:00:00 2001
From: jinghui <181050043@qq.com>
Date: Mon, 26 Oct 2026 21:42:41 +0800
Subject: [PATCH] =?UTF-8?q?feat:=20=E6=9C=AC=E5=9C=B0=E9=95=9C=E5=83=8F?=
=?UTF-8?q?=E6=98=BE=E7=A4=BA=E4=BD=BF=E7=94=A8=E7=8A=B6=E6=80=81=EF=BC=8C?=
=?UTF-8?q?=E8=BF=90=E8=A1=8C=E4=B8=AD=E9=95=9C=E5=83=8F=E4=B8=8D=E5=8F=AF?=
=?UTF-8?q?=E5=88=A0=E9=99=A4?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
CHANGELOG.md | 9 +++++++++
public/app.js | 29 ++++++++++++++++++++++-------
server/index.js | 49 ++++++++++++++++++++++++++++++++++++++++++-------
3 files changed, 73 insertions(+), 14 deletions(-)
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 = `
| 镜像 | ID | 大小 | 创建 | |
- ${imgs.map(i => `| ${(i.tags.length ? i.tags : [':']).map(t => `${esc(t)}`).join(' ')} |
- ${esc(i.shortId)} | ${fmtBytes(i.size)} |
+ 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 | 大小 | 使用情况 | 创建 | |
+ ${imgs.map(i => `
+ | ${(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')} |
- |
`).join('')}
-
`;
+ ${i.canDelete ? `` : '使用中,不可删除'} |
+
`).join('')}
+
+ `;
$$('#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) => {