今天在把windows上使用 docker save [image] > file.tar 导出的镜像导入至Linux平台时报错 archive/tar: invalid tar header。在 Stack Overflow检索后发现是powershell的问题:

I've found it's not a windows specific issue. It's a powershell issue. Powershell emits two byte characters to STDOUT, not one byte characters. If you look in the file you'll notice that the TAR header has nulls between what should be the correct header (and in the rest of the file). This explains why the file is twice the size.
CMD on the other hand does not emit multibyte characters to STDOUT. I've found the STDOUT method of saving a file works fine across different OSes if you use CMD on windows.
——@GiskardReventlov
我发现这不是 Windows 特有的问题。这是一个 Powershell 问题。Powershell 会向 STDOUT 发送两个字节的字符,而不是一个字节的字符。如果你查看文件,就会发现 TAR 头本应是正确的头之间(以及文件的其他部分)有空格。这就解释了为什么文件的大小是原来的两倍。
另一方面,CMD 不会向 STDOUT 发送多字节字符。我发现,如果在 Windows 下使用 CMD,STDOUT 保存文件的方法在不同操作系统下都能正常工作。

因此,在Windows打包Docker镜像时,最好使用docker save [image] -o file.tardocker load -i file.tar

此作者没有提供个人介绍。
最后更新于 2024-12-09