"tar: not found in archive" 使用 docker Alpine 时出错

"tar: not found in archive" error when using docker Alpine

我运行这些命令:

docker run -ti --rm alpine
apk add --no-cache curl
curl https://registry.npmjs.org/monaco-editor/-/monaco-editor-0.15.6.tgz | tar xvz --strip 1 package/min

出现错误 tar: package/min: not found in archive

我运行在Mac终端和dockerubuntu执行同样的命令(curl |tar),都成功了。

Alpine 中包含的 tar 不是通常的 (GNU) tar,而是 BusyBox 的一个组件:

/scratch # tar --version
tar (busybox) 1.28.4

显然,当 运行 和

时,此版本的 tar 会生成(伪造的)错误消息

tar xvz --strip 1 package/min

(但是,乍一看,它创建的 target 目录很好,因此忽略错误消息可能没问题)。

要消除烦人的错误,您应该安装 GNU tar 并使用它:

/scratch # apk add --no-cache tar
fetch http://dl-cdn.alpinelinux.org/alpine/v3.8/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.8/community/x86_64/APKINDEX.tar.gz
(1/1) Installing tar (1.31-r0)
Executing busybox-1.28.4-r2.trigger
OK: 7 MiB in 19 packages
/scratch # tar --version
tar (GNU tar) 1.31
<rest of message omitted>

之后,运行使用您的原始命令,没有任何错误消息。