Skip to content

Commit 5981596

Browse files
lpcoxCopilot
andcommitted
fix: use retry loop with backoff for apt-get update in Dockerfiles
Replace single-retry apt-get update with a 3-attempt retry loop using exponential backoff (10s, 20s, 30s). The single retry was insufficient when Ubuntu mirrors are in prolonged sync states (observed in CI where mirror hash mismatches persisted across multiple minutes). The apt_update_retry function clears the apt cache before each attempt, ensuring a clean state. Applied to all apt-get update calls in both agent and squid Dockerfiles, including the install-retry fallback paths. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 323d397 commit 5981596

2 files changed

Lines changed: 34 additions & 13 deletions

File tree

containers/agent/Dockerfile

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,19 @@ FROM ${BASE_IMAGE}
1111

1212
# Install required packages and Node.js 22
1313
# Note: Some packages may already exist in runner-like base images, apt handles this gracefully
14-
# Retry logic handles transient mirror hash-mismatches and 404s during apt-get update/install
14+
# apt_update_retry: retries up to 3 times with backoff to survive prolonged mirror syncs
1515
RUN set -eux; \
16+
apt_update_retry() { \
17+
local i; for i in 1 2 3; do \
18+
rm -rf /var/lib/apt/lists/* && apt-get update && return 0; \
19+
echo "apt-get update attempt $i/3 failed, retrying in $((i*10))s..." >&2; sleep $((i*10)); \
20+
done; return 1; \
21+
}; \
1622
PKGS="iptables curl ca-certificates git gh gnupg dnsutils net-tools netcat-openbsd gosu libcap2-bin"; \
17-
( apt-get update || (sleep 5 && rm -rf /var/lib/apt/lists/* && apt-get update) ) && \
23+
apt_update_retry && \
1824
( apt-get install -y --no-install-recommends $PKGS || \
1925
(echo "apt-get install failed, retrying with fresh package index..." && \
20-
rm -rf /var/lib/apt/lists/* && \
21-
apt-get update && \
26+
apt_update_retry && \
2227
apt-get install -y --no-install-recommends $PKGS) ) && \
2328
# Prefer system binaries over runner toolcache (e.g., act images) for Node checks.
2429
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:$PATH" && \
@@ -39,23 +44,33 @@ RUN set -eux; \
3944
# These packages are commonly needed by workflows and avoid agents spending time installing them manually
4045
# See: https://github.com/actions/runner-images/blob/main/images/ubuntu/Ubuntu2204-Readme.md
4146
RUN set -eux; \
47+
apt_update_retry() { \
48+
local i; for i in 1 2 3; do \
49+
rm -rf /var/lib/apt/lists/* && apt-get update && return 0; \
50+
echo "apt-get update attempt $i/3 failed, retrying in $((i*10))s..." >&2; sleep $((i*10)); \
51+
done; return 1; \
52+
}; \
4253
PARITY_PKGS="libgdiplus libev-dev libssl-dev php-intl php-gd"; \
43-
( apt-get update || (sleep 5 && rm -rf /var/lib/apt/lists/* && apt-get update) ) && \
54+
apt_update_retry && \
4455
( apt-get install -y --no-install-recommends $PARITY_PKGS || \
4556
(echo "apt-get install failed, retrying with fresh package index..." && \
46-
rm -rf /var/lib/apt/lists/* && \
47-
apt-get update && \
57+
apt_update_retry && \
4858
apt-get install -y --no-install-recommends $PARITY_PKGS) ) && \
4959
rm -rf /var/lib/apt/lists/*
5060

5161
# Upgrade all packages to pick up security patches
5262
# Addresses CVE-2023-44487 (HTTP/2 Rapid Reset) and other known vulnerabilities
5363
# Retry logic handles transient mirror sync failures during apt-get update
54-
RUN ( apt-get update || (sleep 5 && rm -rf /var/lib/apt/lists/* && apt-get update) ) && \
64+
RUN apt_update_retry() { \
65+
local i; for i in 1 2 3; do \
66+
rm -rf /var/lib/apt/lists/* && apt-get update && return 0; \
67+
echo "apt-get update attempt $i/3 failed, retrying in $((i*10))s..." >&2; sleep $((i*10)); \
68+
done; return 1; \
69+
}; \
70+
apt_update_retry && \
5571
apt-get upgrade -y && rm -rf /var/lib/apt/lists/* || \
5672
(echo "apt-get upgrade failed, retrying with fresh package index..." && \
57-
rm -rf /var/lib/apt/lists/* && \
58-
apt-get update && apt-get upgrade -y && rm -rf /var/lib/apt/lists/*)
73+
apt_update_retry && apt-get upgrade -y && rm -rf /var/lib/apt/lists/*)
5974

6075
# Create non-root user with UID/GID matching host user
6176
# This allows the user command to run with appropriate permissions

containers/squid/Dockerfile

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
FROM ubuntu/squid:latest
22

33
# Install additional tools for debugging, healthcheck, and SSL Bump
4-
# Retry logic handles transient mirror hash-mismatches and 404s during apt-get update/install
4+
# apt_update_retry: retries up to 3 times with backoff to survive prolonged mirror syncs
55
RUN set -eux; \
6+
apt_update_retry() { \
7+
local i; for i in 1 2 3; do \
8+
rm -rf /var/lib/apt/lists/* && apt-get update && return 0; \
9+
echo "apt-get update attempt $i/3 failed, retrying in $((i*10))s..." >&2; sleep $((i*10)); \
10+
done; return 1; \
11+
}; \
612
PKGS="curl dnsutils net-tools netcat-openbsd openssl squid-openssl"; \
7-
( apt-get update || (sleep 5 && rm -rf /var/lib/apt/lists/* && apt-get update) ) && \
13+
apt_update_retry && \
814
apt-get install -y --only-upgrade gpgv && \
915
( apt-get install -y --no-install-recommends $PKGS || \
10-
(rm -rf /var/lib/apt/lists/* && apt-get update && \
16+
(apt_update_retry && \
1117
apt-get install -y --no-install-recommends $PKGS) ) && \
1218
rm -rf /var/lib/apt/lists/*
1319

0 commit comments

Comments
 (0)