apt-get 更新失败,但退出状态仍然是成功,这破坏了我的 docker 缓存

apt-get update failed, but exit status is still success, this broke my docker cache

这里是docker构建日志

的重要部分
#6 [stage-1  2/20] RUN apt-get -y update && apt-get -y upgrade
#6 sha256:6a65c591472319f4cf0c6954f890ee1d5e465b9b12670d78dd15e738cf4bd37d
#6 31.57 Err:1 http://archive.ubuntu.com/ubuntu focal InRelease
#6 31.57   Could not connect to archive.ubuntu.com:80 (91.189.88.142), connection timed out Could not connect to archive.ubuntu.com:80 (91.189.88.152), connection timed out
#6 31.57 Err:2 http://archive.ubuntu.com/ubuntu focal-updates InRelease
#6 31.57   Unable to connect to archive.ubuntu.com:http:
#6 31.57 Err:3 http://archive.ubuntu.com/ubuntu focal-backports InRelease
#6 31.57   Unable to connect to archive.ubuntu.com:http:
#6 32.00 Err:4 http://security.ubuntu.com/ubuntu focal-security InRelease
#6 32.00   Could not connect to security.ubuntu.com:80 (91.189.88.152), connection timed out Could not connect to security.ubuntu.com:80 (91.189.91.39), connection timed out Could not connect to security.ubuntu.com:80 (91.189.88.142), connection timed out Could not connect to security.ubuntu.com:80 (91.189.91.38), connection timed out
#6 32.02 Reading package lists...
#6 32.04 W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/focal/InRelease  Could not connect to archive.ubuntu.com:80 (91.189.88.142), connection timed out Could not connect to archive.ubuntu.com:80 (91.189.88.152), connection timed out
#6 32.04 W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/focal-updates/InRelease  Unable to connect to archive.ubuntu.com:http:
#6 32.04 W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/focal-backports/InRelease  Unable to connect to archive.ubuntu.com:http:
#6 32.04 W: Failed to fetch http://security.ubuntu.com/ubuntu/dists/focal-security/InRelease  Could not connect to security.ubuntu.com:80 (91.189.88.152), connection timed out Could not connect to security.ubuntu.com:80 (91.189.91.39), connection timed out Could not connect to security.ubuntu.com:80 (91.189.88.142), connection timed out Could not connect to security.ubuntu.com:80 (91.189.91.38), connection timed out
#6 32.04 W: Some index files failed to download. They have been ignored, or old ones used instead.
#6 32.06 Reading package lists...
#6 32.08 Building dependency tree...
#6 32.08 Reading state information...
#6 32.08 Calculating upgrade...
#6 32.08 0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
#6 DONE 32.3s

如您所见,存在一些错误,但出于某种原因 docker 认为两个命令都成功结束:#6 DONE 32.3s.

这损坏了我的 Docker 缓存并在构建机器上造成了问题。

问题

现在的问题不是如何修复这一步,这是 other SO questions (other) 的内容。

如何防止这个问题并强制apt-get 报告错误(none 零退出代码)。在我的例子中,问题是暂时的网络问题和这个损坏的 docker 缓存。我已清除缓存以解决此问题,但如何防止此问题在将来重复出现?

这些不被视为错误,而是警告,正如您在该行前面的 W 所见。

您可以通过添加以下 -o APT::Update::Error-Mode=any

apt-get update 失败

POSIX 操作系统(linux,unix....)上的任何失败命令 returns 代码,0 表示成功,其他任何错误。

此值存储在“$?”中变量,但您可以使用 || 直接分支您的 shell 脚本for or and && for an and.

apt upgrade || echo "\e[31mERROR running APT \!\e[0m"

\e 为您提供各种格式选项颜色、粗体、下划线等...https://misc.flogisoft.com/bash/tip_colors_and_formatting