feat: 镜像导出 tar.gz + 未启动应用整卡点击启动
This commit is contained in:
1 parent
f8ed288b5e
commit
fc51ac5b62
4 files changed
+69
-9
No files matched your search
@@ -1,5 +1,14 @@
|
||||
# 更新日志
|
||||
|
||||
## 2026-10-26 → ✨ 镜像导出 + 未启动应用整卡点击启动
|
||||
|
||||
- ✨ 本地镜像每行新增「导出」按钮:`GET /api/images/:id/export` 用 `docker save` 流式导出并 gzip 压缩,文件名自动取镜像 tag(如 `docker.m.daocloud.io_library_hello-world_latest.tar.gz`)
|
||||
- ✨ 导出对大镜像友善:边读边压边发,不落临时文件;连接中断自动销毁流
|
||||
- ✨ 被运行中容器占用的镜像也能导出(导出与删除权限分离)
|
||||
- ✨ 「有镜像但未启动的应用」卡片**整卡可点**(鼠标手型 + 悬浮上浮效果),点击即 `docker compose up -d`;卡片内「启动」按钮同样可用,不再需要点小按钮
|
||||
- 🔧 启动中按钮显示「启动中…」,防重复点击;成功后 1.2 秒自动刷新
|
||||
- ✅ 实测:导出 hello-world 得到 9 个文件的合法 tar.gz(OCI 布局),响应头 `Content-Disposition` 文件名正确;点 `pendclick-check` 卡片后容器 `Up`、卡片从待启动列表消失
|
||||
|
||||
## 2026-10-26 → ✨ 本地镜像显示使用状态,运行中镜像不可删除
|
||||
|
||||
- ✨ 镜像表格新增「使用情况」列:`运行中 N 个容器`(红)/ `已停止容器引用 N`(黄)/ `未使用`(灰),悬停显示具体容器名
|
||||
|
||||
+28
-9
@@ -629,7 +629,7 @@ async function renderContainers(silent, mode = 'running') {
|
||||
<div class="sec-head"><b>有镜像但未启动的应用</b><span class="muted">${pend.items.length} 个 · 扫描 ${esc((pend.root || '').replace(/^.*\/docker$/, '.../docker'))}</span></div>
|
||||
<div class="pend-grid">
|
||||
${pend.items.map(p => `
|
||||
<div class="pend-card">
|
||||
<div class="pend-card act" data-pend-card="${esc(p.name)}" data-file="${esc(p.file)}" title="点击启动该应用">
|
||||
<div class="pend-top"><b title="${esc(p.name)}">${esc(p.name)}</b><span class="tag">${p.services} 个服务</span></div>
|
||||
<div class="pend-sub" title="${esc(p.dir)}">${esc(p.file.replace((pend.root || '') + '/', ''))}</div>
|
||||
<div class="pend-img">${p.images.missing.length
|
||||
@@ -657,15 +657,23 @@ async function renderContainers(silent, mode = 'running') {
|
||||
$$('#view [data-pend-cmd]').forEach(b => b.onclick = () => {
|
||||
alert(`在宿主机执行:\n\ndocker compose -f ${b.dataset.pendCmd} up -d`);
|
||||
});
|
||||
$$('#view [data-pend]').forEach(b => b.onclick = async () => {
|
||||
const name = b.dataset.pend;
|
||||
if (!confirm(`启动应用「${name}」?\n\n将执行 docker compose up -d,会创建并运行该项目下的容器。`)) return;
|
||||
b.disabled = true; b.textContent = '启动中…';
|
||||
const startPend = async (name, el) => {
|
||||
if (el) { el.disabled = true; el.dataset.txt = el.textContent; el.textContent = '启动中…'; }
|
||||
try {
|
||||
const r = await api('/api/pending-apps/start', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name }) });
|
||||
if (r.ok) { toast(`「${name}」已启动`); setTimeout(() => render(), 1500); }
|
||||
else { alert(`启动失败:\n\n${r.output || '未知错误'}`); b.disabled = false; b.textContent = '启动'; }
|
||||
} catch (e) { toast(e.message, true); b.disabled = false; b.textContent = '启动'; }
|
||||
toast(r.message || `已启动 ${name}`);
|
||||
setTimeout(render, 1200);
|
||||
} catch (e) {
|
||||
toast(e.message, true);
|
||||
if (el) { el.disabled = false; el.textContent = el.dataset.txt || '启动'; }
|
||||
}
|
||||
};
|
||||
$$('#view [data-pend]').forEach(b => b.onclick = (e) => { e.stopPropagation(); startPend(b.dataset.pend, b); });
|
||||
$$('#view [data-pend-card]').forEach(card => {
|
||||
card.onclick = (e) => {
|
||||
if (e.target.closest('[data-pend]') || e.target.closest('[data-pend-cmd]')) return;
|
||||
startPend(card.dataset.pendCard, card.querySelector('[data-pend]'));
|
||||
};
|
||||
});
|
||||
|
||||
if (mode === 'stopped') {
|
||||
@@ -869,10 +877,21 @@ async function renderImages() {
|
||||
<td class="num">${fmtBytes(i.size)}</td>
|
||||
<td>${useCell(i)}</td>
|
||||
<td class="muted">${new Date(i.created * 1000).toLocaleDateString('zh-CN')}</td>
|
||||
<td>${i.canDelete ? `<button class="btn danger" data-img="${esc(i.id)}">删除</button>` : '<span class="muted" style="font-size:11.5px">使用中,不可删除</span>'}</td>
|
||||
<td class="img-acts">
|
||||
<button class="btn ghost sm" data-export="${esc(i.id)}" title="导出为 tar.gz(docker save)">导出</button>
|
||||
${i.canDelete ? `<button class="btn danger sm" 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-export]').forEach(b => b.onclick = () => {
|
||||
const id = b.dataset.export;
|
||||
const a = document.createElement('a');
|
||||
a.href = `/api/images/${encodeURIComponent(id)}/export`;
|
||||
a.download = '';
|
||||
document.body.appendChild(a); a.click(); a.remove();
|
||||
toast('开始导出,浏览器下载中…(大镜像可能需要 1-2 分钟)');
|
||||
});
|
||||
$$('#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(); }
|
||||
|
||||
@@ -333,3 +333,13 @@ table.tb th.num{text-align:right}
|
||||
.batch-bar .btn[disabled]{opacity:.5;cursor:not-allowed}
|
||||
.chk{display:flex;align-items:center;margin-right:2px;flex-shrink:0}
|
||||
.chk input{width:15px;height:15px;cursor:pointer;accent-color:#e5484d}
|
||||
|
||||
/* 镜像导出 / 未启动应用整卡点击 */
|
||||
.btn.sm{font-size:11.5px;padding:3px 9px;height:26px}
|
||||
.btn.ghost{background:#fff;border:1px solid #dfe6ef;color:#5a6b82}
|
||||
.btn.ghost:hover{background:#f5f8fc;border-color:#c9d5e4;color:#2d3b4f}
|
||||
.img-acts{white-space:nowrap;text-align:right}
|
||||
.img-acts .btn + .btn{margin-left:6px}
|
||||
.pend-card.act{cursor:pointer;transition:box-shadow .15s,border-color .15s,transform .15s}
|
||||
.pend-card.act:hover{border-color:#c9d8ee;box-shadow:0 4px 14px rgba(26,115,232,.12);transform:translateY(-1px)}
|
||||
.pend-card.act:active{transform:translateY(0)}
|
||||
@@ -7,6 +7,7 @@ const fsp = require('fs/promises');
|
||||
const path = require('path');
|
||||
const os = require('os');
|
||||
const { PassThrough } = require('stream');
|
||||
const zlib = require('zlib');
|
||||
const { execFile } = require('child_process');
|
||||
|
||||
const docker = new Docker({ socketPath: process.env.DOCKER_SOCK || '/var/run/docker.sock' });
|
||||
@@ -1058,6 +1059,27 @@ app.get('/api/images', async (req, res) => {
|
||||
} catch (e) { res.status(500).json({ error: e.message }); }
|
||||
});
|
||||
|
||||
app.get('/api/images/:id/export', async (req, res) => {
|
||||
try {
|
||||
const img = await docker.getImage(req.params.id).inspect();
|
||||
const tag = (img.RepoTags && img.RepoTags[0]) || '';
|
||||
const base = tag ? tag.replace(/[^a-zA-Z0-9._-]+/g, '_').replace(/^_+|_+$/g, '') : img.Id.replace('sha256:', '').slice(0, 12);
|
||||
const name = `${base || 'image'}.tar.gz`;
|
||||
res.setHeader('Content-Type', 'application/gzip');
|
||||
res.setHeader('Content-Disposition', `attachment; filename="${name}"`);
|
||||
res.setHeader('X-Image-Name', name);
|
||||
const stream = await docker.getImage(img.Id).get();
|
||||
const gz = zlib.createGzip({ level: 3 });
|
||||
stream.on('error', (e) => { try { res.end(); } catch (_) {} });
|
||||
gz.on('error', () => { try { res.end(); } catch (_) {} });
|
||||
res.on('close', () => { try { stream.destroy(); gz.destroy(); } catch (_) {} });
|
||||
stream.pipe(gz).pipe(res);
|
||||
} catch (e) {
|
||||
if (!res.headersSent) res.status(500).json({ error: e.message });
|
||||
else try { res.end(); } catch (_) {}
|
||||
}
|
||||
});
|
||||
|
||||
app.delete('/api/images/:id', async (req, res) => {
|
||||
try {
|
||||
const id = req.params.id;
|
||||
|
||||
Reference in new issue
Block a user