feat: 本地镜像支持导入 tar/tar.gz

This commit is contained in:
jinghui committed 2026-10-26 22:33:39 +08:00
1 parent 1791e94028
commit 4ade1439a3
3 files changed
+64

No files matched your search

+8
View File
@@ -1,5 +1,13 @@
# 更新日志
## 2026-10-26 → ✨ 本地镜像支持导入
- ✨ 本地镜像页批量条新增「导入镜像」:选择 `docker save` 导出的 `.tar` / `.tar.gz` / `.tgz` 即可导入,按钮实时显示「上传 42%」
- ✨ 导入完成后提示具体镜像名(如「已导入 1 个镜像:docker.m.daocloud.io/library/hello-world:latest」),列表自动刷新
- 🔧 新增 `POST /api/images/import`:**不落盘、不占内存**,直接把上传流 pipe 到 Docker socket 的 `/images/load`,支持多镜像包
- 🔧 错误处理:非镜像包返回 400「文件不是有效的镜像包(需要 docker save 导出的 tar 或 tar.gz)」,不再默默显示成功
- ✅ 实测:gzip 包导入 hello-world 成功并在列表出现;纯 tar(8.4MB alpine)导入成功;随机垃圾文件返回 400 友好提示
## 2026-10-26 → ✨ 新增「备份与恢复」页
- ✨ 侧栏新增「备份与恢复」:勾选要备份的内容 → 立即备份 → 生成 `docker-center-backup-<时间戳>.tar.gz`
+32
View File
@@ -930,6 +930,8 @@ async function renderImages() {
<span class="muted">已选 <b id="imgSelCount">${[...state.imgSel].filter(id => imgs.some(i => i.id === id)).length}</b> / ${imgs.length}</span>
<span class="muted">批量导出会打包成一个 tar.gz</span>
<button class="btn primary" id="imgExportSel" disabled>导出选中</button>
<button class="btn ghost" id="imgImport">导入镜像</button>
<input type="file" id="imgFile" accept=".tar,.gz,.tgz,.tar.gz" style="display:none" />
</div>`;
$('#view').innerHTML = `${batchBar}<table class="tb"><thead><tr><th class="chkcell"></th><th>镜像</th><th>ID</th><th class="num">大小</th><th>使用情况</th><th>创建</th><th></th></tr></thead><tbody>
${imgs.map(i => `<tr>
@@ -962,6 +964,36 @@ async function renderImages() {
$$('#view .imgsel').forEach(cb => { cb.checked = sa.checked; });
syncImgSel();
};
const imp = $('#imgImport');
if (imp) imp.onclick = () => $('#imgFile').click();
const fileInp = $('#imgFile');
if (fileInp) fileInp.onchange = () => {
const f = fileInp.files && fileInp.files[0];
if (!f) return;
const mb = (f.size / 1048576).toFixed(1);
if (!confirm(`导入镜像文件 ${f.name}(${mb} MB)?\n\n支持 docker save 导出的 .tar / .tar.gz,导入期间请勿关闭页面。`)) { fileInp.value = ''; return; }
const btn = $('#imgImport');
btn.disabled = true;
const xhr = new XMLHttpRequest();
xhr.open('POST', '/api/images/import');
xhr.setRequestHeader('Content-Type', 'application/octet-stream');
xhr.upload.onprogress = (e) => {
if (e.lengthComputable) btn.textContent = `上传 ${Math.round((e.loaded / e.total) * 100)}%`;
};
xhr.onload = () => {
btn.disabled = false; btn.textContent = '导入镜像';
fileInp.value = '';
try {
const r = JSON.parse(xhr.responseText);
if (xhr.status === 200 && r.ok) {
toast(r.count ? `已导入 ${r.count} 个镜像:${r.loaded.slice(0, 3).join('、')}` : '导入完成(未识别到镜像名)');
setTimeout(render, 800);
} else toast(r.error || '导入失败', true);
} catch (e) { toast('导入失败:' + xhr.responseText.slice(0, 120), true); }
};
xhr.onerror = () => { btn.disabled = false; btn.textContent = '导入镜像'; toast('导入失败(网络错误)', true); };
xhr.send(f);
};
const be = $('#imgExportSel');
if (be) be.onclick = () => {
let list = [...state.imgSel];
+24
View File
@@ -1135,6 +1135,30 @@ app.get('/api/images', async (req, res) => {
} catch (e) { res.status(500).json({ error: e.message }); }
});
app.post('/api/images/import', (req, res) => {
const sock = process.env.DOCKER_SOCK || '/var/run/docker.sock';
const up = http.request({
socketPath: sock, path: '/images/load?quiet=0', method: 'POST',
headers: { 'Content-Type': 'application/x-tar', 'Transfer-Encoding': 'chunked' },
}, (r) => {
let buf = '';
r.on('data', (c) => { buf += c.toString('utf8'); });
r.on('end', () => {
const errLine = buf.split(/\r?\n/).map(l => { try { return JSON.parse(l); } catch (_) { return null; } }).find(o => o && (o.error || o.errorDetail));
if (r.statusCode !== 200 || errLine) {
const msg = errLine ? ((errLine.errorDetail && errLine.errorDetail.message) || errLine.error) : buf.slice(-300);
return res.status(400).json({ error: /invalid tar header|unexpected EOF/i.test(String(msg)) ? '文件不是有效的镜像包(需要 docker save 导出的 tar 或 tar.gz)' : '导入失败:' + msg });
}
const loaded = [...buf.matchAll(/Loaded image:?\s*([^\s"\\]+)/g)].map(m => m[1]);
pushEvent('info', null, `导入镜像 ${loaded.length} 个`, loaded.slice(0, 3).join('、'));
res.json({ ok: true, loaded, count: loaded.length, raw: buf.slice(-800) });
});
});
up.on('error', (e) => { if (!res.headersSent) res.status(500).json({ error: e.message }); else res.end(); });
req.on('aborted', () => up.destroy());
req.pipe(up);
});
app.get('/api/images/export', async (req, res) => {
try {
const ids = String(req.query.ids || '').split(',').map(x => x.trim()).filter(Boolean).slice(0, 40);