Skip to content

Commit d241a43

Browse files
Improve Docker downgrade robustness and version reporting (#72)
* Improve Docker downgrade robustness and version reporting Enhances the downgrade_docker function to retry apt-get installs, verify package installation after errors, and provide more detailed status and version reporting for containerd. Also updates package hold/unhold logic to include docker-ce-rootless-extras and improves logging in both run.sh and test-script.sh for better diagnostics and validation. * Refactor variable assignment and improve containerd version check Refactored variable assignments in run.sh and test-script.sh to use separate declaration and assignment for better readability and shell compatibility. Updated the containerd version check in test-script.sh to accept both '1.7.28' and 'v1.7.28' formats.
1 parent bf8775e commit d241a43

2 files changed

Lines changed: 159 additions & 48 deletions

File tree

casaos-fix-docker-api-version/run.sh

Lines changed: 127 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ print_info() {
5959
}
6060

6161
echo "=========================================="
62-
echo "BigBear CasaOS Docker Version Fix Script 2025.12.0"
62+
echo "BigBear CasaOS Docker Version Fix Script 2025.12.1"
6363
echo "=========================================="
6464
echo ""
6565
echo "Here are some links:"
@@ -1110,7 +1110,7 @@ downgrade_docker() {
11101110

11111111
# Hold Docker packages to prevent auto-upgrade
11121112
echo "Configuring Docker packages to prevent auto-upgrade..."
1113-
$SUDO apt-mark unhold docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin 2>/dev/null || true
1113+
$SUDO apt-mark unhold docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker-ce-rootless-extras 2>/dev/null || true
11141114
echo ""
11151115

11161116
# Check if Docker is already installed and remove it to ensure clean downgrade
@@ -1124,7 +1124,7 @@ downgrade_docker() {
11241124
timeout 30 $SUDO systemctl stop containerd 2>/dev/null || true
11251125
sleep 2
11261126

1127-
$SUDO apt-get remove --purge -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin 2>/dev/null || true
1127+
$SUDO apt-get remove --purge -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker-ce-rootless-extras 2>/dev/null || true
11281128

11291129
# Clean up unused dependencies
11301130
echo "Cleaning up unused dependencies..."
@@ -1195,46 +1195,100 @@ downgrade_docker() {
11951195
# Use timeout (10 minutes max for download/install)
11961196
# Prevent services from starting during install to avoid hangs
11971197
prevent_service_autostart
1198-
1199-
if ! timeout 600 $SUDO apt-get install -y --allow-downgrades \
1200-
docker-ce=${DOCKER_VERSION} \
1201-
docker-ce-cli=${DOCKER_VERSION} \
1202-
containerd.io=${CONTAINERD_FULL} \
1203-
docker-buildx-plugin \
1204-
docker-compose-plugin 2>&1 | tee /tmp/docker-install.log; then
1205-
allow_service_autostart
1206-
if [ ${PIPESTATUS[0]} -eq 124 ]; then
1207-
echo ""
1208-
echo "ERROR: Docker installation timed out after 10 minutes!"
1209-
echo "This usually means:"
1210-
echo " - Very slow network connection"
1211-
echo " - Repository mirror is overloaded or broken"
1212-
echo " - Package download is stuck"
1213-
echo ""
1214-
echo "Suggestion: Try the override.conf method instead to keep your current Docker"
1215-
else
1216-
echo ""
1217-
echo "ERROR: Docker installation failed!"
1218-
echo "Please check your internet connection and try again."
1219-
echo ""
1220-
echo "Attempted to install:"
1221-
echo " docker-ce=${DOCKER_VERSION}"
1222-
echo " docker-ce-cli=${DOCKER_VERSION}"
1223-
echo " containerd.io=${CONTAINERD_FULL}"
1224-
echo ""
1225-
echo "Installation log saved to: /tmp/docker-install.log"
1226-
fi
1227-
return 1
1198+
# Ensure we always restore policy-rc.d even on early return
1199+
trap "allow_service_autostart" RETURN
1200+
1201+
local install_attempt=1
1202+
local max_install_attempts=2
1203+
local apt_success=false
1204+
1205+
while [ $install_attempt -le $max_install_attempts ]; do
1206+
echo "apt-get install attempt ${install_attempt}/${max_install_attempts}..."
1207+
if timeout 600 $SUDO apt-get install -y --allow-downgrades \
1208+
docker-ce=${DOCKER_VERSION} \
1209+
docker-ce-cli=${DOCKER_VERSION} \
1210+
containerd.io=${CONTAINERD_FULL} \
1211+
docker-buildx-plugin \
1212+
docker-compose-plugin 2>&1 | tee /tmp/docker-install.log; then
1213+
apt_success=true
1214+
break
1215+
fi
1216+
1217+
echo ""
1218+
echo "⚠ apt-get returned an error, verifying package installation..."
1219+
1220+
local docker_installed=false
1221+
local containerd_installed=false
1222+
1223+
# Check if docker-ce was installed with correct version
1224+
if dpkg -l docker-ce 2>/dev/null | grep -q "${DOCKER_VERSION}"; then
1225+
echo "✓ docker-ce ${DOCKER_VERSION} is installed"
1226+
docker_installed=true
1227+
fi
1228+
1229+
# Check if containerd.io was installed with correct version
1230+
if dpkg -l containerd.io 2>/dev/null | grep -q "${CONTAINERD_FULL}"; then
1231+
echo "✓ containerd.io ${CONTAINERD_FULL} is installed"
1232+
containerd_installed=true
1233+
fi
1234+
1235+
# If both packages are installed correctly, treat as success
1236+
if [ "$docker_installed" = true ] && [ "$containerd_installed" = true ]; then
1237+
echo ""
1238+
echo "✓ Packages were installed successfully despite apt-get warnings"
1239+
echo "This is usually due to non-critical post-install script issues"
1240+
echo ""
1241+
apt_success=true
1242+
break
1243+
fi
1244+
1245+
if [ $install_attempt -lt $max_install_attempts ]; then
1246+
echo "Retrying apt-get install in 5 seconds..."
1247+
sleep 5
1248+
fi
1249+
install_attempt=$((install_attempt + 1))
1250+
done
1251+
1252+
if [ "$apt_success" != true ]; then
1253+
# Actual installation failure
1254+
if [ ${PIPESTATUS[0]} -eq 124 ]; then
1255+
echo ""
1256+
echo "ERROR: Docker installation timed out after 10 minutes!"
1257+
echo "This usually means:"
1258+
echo " - Very slow network connection"
1259+
echo " - Repository mirror is overloaded or broken"
1260+
echo " - Package download is stuck"
1261+
echo ""
1262+
echo "Suggestion: Try the override.conf method instead to keep your current Docker"
1263+
else
1264+
echo ""
1265+
echo "ERROR: Docker installation failed!"
1266+
echo "Please check your internet connection and try again."
1267+
echo ""
1268+
echo "Attempted to install:"
1269+
echo " docker-ce=${DOCKER_VERSION}"
1270+
echo " docker-ce-cli=${DOCKER_VERSION}"
1271+
echo " containerd.io=${CONTAINERD_FULL}"
1272+
echo ""
1273+
echo "Installation log saved to: /tmp/docker-install.log"
1274+
fi
1275+
return 1
12281276
fi
12291277

1278+
echo ""
1279+
echo "✓ Package installation completed successfully"
1280+
echo "Now restoring service auto-start and configuring Docker..."
1281+
echo ""
1282+
12301283
allow_service_autostart
12311284

12321285
echo "✓ Successfully installed Docker 28.x (API 1.47/1.48)"
1286+
echo "✓ Service auto-start policy restored"
12331287
echo ""
12341288

12351289
# Hold packages to prevent auto-upgrade
12361290
echo "Holding Docker packages at current version..."
1237-
$SUDO apt-mark hold docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
1291+
$SUDO apt-mark hold docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker-ce-rootless-extras
12381292
echo ""
12391293

12401294
# Verify the dockerd binary version before starting
@@ -1256,10 +1310,11 @@ downgrade_docker() {
12561310
echo ""
12571311

12581312
# Reload systemd and restart Docker service
1259-
echo "Reloading systemd daemon..."
1313+
echo "Step 7.0: Reloading systemd daemon..."
12601314
$SUDO systemctl daemon-reload
12611315
# Wait for systemd to fully process the reload
12621316
sleep 2
1317+
echo "✓ Systemd daemon reloaded"
12631318
echo ""
12641319

12651320
# Ensure Docker is completely stopped before starting
@@ -1271,32 +1326,51 @@ downgrade_docker() {
12711326
# Use the new function to ensure processes are stopped
12721327
ensure_docker_processes_stopped
12731328

1274-
# Stop containerd to ensure clean slate
1275-
echo "Restarting containerd for clean state..."
1329+
# Stop containerd completely to ensure new version is loaded
1330+
echo "Step 7.2.1: Restarting containerd for clean state..."
1331+
echo "Stopping containerd service..."
12761332
timeout 30 $SUDO systemctl stop containerd 2>/dev/null || true
12771333
sleep 2
1334+
1335+
# Kill any lingering containerd processes to ensure old binary is not running
12781336
if pgrep -x containerd >/dev/null 2>&1; then
1337+
echo "Forcefully stopping lingering containerd processes (to load new version)..."
12791338
$SUDO pkill -9 containerd 2>/dev/null || true
1339+
sleep 3
1340+
fi
1341+
1342+
# Kill containerd-shim processes too
1343+
if pgrep containerd-shim >/dev/null 2>&1; then
1344+
echo "Stopping containerd-shim processes..."
1345+
$SUDO pkill -9 containerd-shim 2>/dev/null || true
12801346
sleep 2
12811347
fi
1348+
1349+
echo "Starting containerd with new version..."
12821350
$SUDO systemctl start containerd 2>/dev/null || true
12831351

12841352
# Wait for containerd to be fully ready before starting Docker
12851353
echo "Waiting for containerd to be fully ready..."
12861354
local containerd_ready=false
1287-
for i in {1..10}; do
1355+
for i in {1..15}; do
12881356
if $SUDO systemctl is-active --quiet containerd 2>/dev/null; then
12891357
containerd_ready=true
12901358
echo "✓ containerd is ready"
12911359
break
12921360
fi
1293-
echo " Waiting for containerd... ($i/10)"
1361+
echo " Waiting for containerd... ($i/15)"
12941362
sleep 1
12951363
done
12961364

12971365
if [ "$containerd_ready" = false ]; then
12981366
echo "⚠ WARNING: containerd may not be fully ready, proceeding anyway..."
12991367
fi
1368+
1369+
# Verify containerd binary version after restart
1370+
echo "Verifying containerd version..."
1371+
local containerd_version
1372+
containerd_version=$(timeout 5 containerd --version 2>/dev/null | head -n1 || echo "unknown")
1373+
echo "containerd binary: $containerd_version"
13001374
echo ""
13011375

13021376
# Enable and start docker socket first, then service
@@ -1338,10 +1412,10 @@ downgrade_docker() {
13381412
echo "=========================================="
13391413
echo ""
13401414
echo "Checking Docker status and logs..."
1341-
timeout 10 $SUDO systemctl status docker --no-pager -l || true
1415+
timeout 10 $SUDO systemctl status docker --no-pager -l | tee -a /tmp/docker-install.log || true
13421416
echo ""
13431417
echo "Recent Docker logs:"
1344-
timeout 10 $SUDO journalctl -u docker --no-pager -n 50 || true
1418+
timeout 10 $SUDO journalctl -u docker --no-pager -n 50 | tee -a /tmp/docker-install.log || true
13451419
echo ""
13461420

13471421
# Attempt to detect and fix the error
@@ -1638,23 +1712,23 @@ main() {
16381712
case "$1" in
16391713
apply-override|override)
16401714
echo "=========================================="
1641-
echo "BigBear CasaOS Docker Version Fix Script 2025.12.0"
1715+
echo "BigBear CasaOS Docker Version Fix Script 2025.12.1"
16421716
echo "=========================================="
16431717
echo ""
16441718
apply_docker_api_override
16451719
exit $?
16461720
;;
16471721
remove-override|no-override)
16481722
echo "=========================================="
1649-
echo "BigBear CasaOS Docker Version Fix Script 2025.12.0"
1723+
echo "BigBear CasaOS Docker Version Fix Script 2025.12.1"
16501724
echo "=========================================="
16511725
echo ""
16521726
remove_docker_api_override
16531727
exit $?
16541728
;;
16551729
help|--help|-h)
16561730
echo "=========================================="
1657-
echo "BigBear CasaOS Docker Version Fix Script 2025.12.0"
1731+
echo "BigBear CasaOS Docker Version Fix Script 2025.12.1"
16581732
echo "=========================================="
16591733
echo ""
16601734
show_usage
@@ -2086,9 +2160,18 @@ main() {
20862160
echo "Docker Configuration Complete!"
20872161
echo "=========================================="
20882162
echo ""
2163+
2164+
local containerd_pkg_version
2165+
containerd_pkg_version=$(dpkg -l containerd.io 2>/dev/null | awk 'NR>5 {print $3; exit}')
2166+
local containerd_bin_version
2167+
containerd_bin_version=$(timeout 5 containerd --version 2>/dev/null | head -n1)
2168+
20892169
echo "Installed Docker Package Versions:"
20902170
timeout 10 dpkg -l 2>/dev/null | grep -E "docker-ce|containerd.io" | awk '{print " " $2 " = " $3}' || echo "Unable to query package versions"
20912171
echo ""
2172+
echo "Containerd Package Version: ${containerd_pkg_version:-unknown}"
2173+
echo "Containerd Binary Version: ${containerd_bin_version:-Unable to get containerd binary version}"
2174+
echo ""
20922175
echo "Docker Version Information:"
20932176
timeout 10 $SUDO docker version 2>&1 || echo "Unable to get Docker version"
20942177
echo ""

casaos-fix-docker-api-version/test-script.sh

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,16 @@ show_status() {
9797
local docker_version=$(get_docker_version)
9898
local api_version=$(get_docker_api_version)
9999
local dockerd_version=$(get_dockerd_binary_version)
100+
local containerd_pkg
101+
containerd_pkg=$(dpkg -l 2>/dev/null | grep containerd.io | awk 'NR==1 {print $3}')
102+
local containerd_bin
103+
containerd_bin=$(containerd --version 2>/dev/null | awk '{print $3}')
100104

101105
echo "Docker daemon version: $docker_version"
102106
echo "Docker API version: $api_version"
103107
echo "dockerd binary version: $dockerd_version"
108+
echo "containerd package: ${containerd_pkg:-unknown}"
109+
echo "containerd binary: ${containerd_bin:-unknown}"
104110
echo ""
105111

106112
# Show package versions
@@ -303,7 +309,7 @@ upgrade_docker_to_latest() {
303309

304310
# Unhold packages if they're held
305311
print_info "Unholding Docker packages..."
306-
$SUDO apt-mark unhold docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin 2>/dev/null || true
312+
$SUDO apt-mark unhold docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker-ce-rootless-extras 2>/dev/null || true
307313
echo ""
308314

309315
# Setup Docker repository if not already present
@@ -337,7 +343,8 @@ upgrade_docker_to_latest() {
337343
docker-ce-cli \
338344
containerd.io \
339345
docker-buildx-plugin \
340-
docker-compose-plugin; then
346+
docker-compose-plugin \
347+
docker-ce-rootless-extras; then
341348
print_success "Docker upgraded to latest version"
342349
else
343350
print_error "Failed to upgrade Docker"
@@ -420,10 +427,16 @@ test_fix_script() {
420427
local after_version=$(get_docker_version)
421428
local after_api=$(get_docker_api_version)
422429
local dockerd_binary=$(get_dockerd_binary_version)
430+
local containerd_pkg
431+
containerd_pkg=$(dpkg -l 2>/dev/null | grep containerd.io | awk 'NR==1 {print $3}')
432+
local containerd_bin
433+
containerd_bin=$(containerd --version 2>/dev/null | awk '{print $3}')
423434

424435
echo "Docker version after fix: $after_version"
425436
echo "API version after fix: $after_api"
426437
echo "dockerd binary version: $dockerd_binary"
438+
echo "containerd package: ${containerd_pkg:-unknown}"
439+
echo "containerd binary: ${containerd_bin:-unknown}"
427440
echo ""
428441

429442
# Check package versions
@@ -458,6 +471,20 @@ test_fix_script() {
458471
print_error "✗ dockerd binary is NOT 28.x (got: $dockerd_binary)"
459472
success=false
460473
fi
474+
475+
# Check containerd package/bin match expected 1.7.28
476+
if echo "$containerd_pkg" | grep -q "1.7.28"; then
477+
print_success "✓ containerd package is 1.7.28 (${containerd_pkg})"
478+
else
479+
print_error "✗ containerd package is not 1.7.28 (got: ${containerd_pkg:-unknown})"
480+
success=false
481+
fi
482+
if echo "$containerd_bin" | grep -q "^v\?1\.7\.28"; then
483+
print_success "✓ containerd binary is 1.7.28 (${containerd_bin})"
484+
else
485+
print_error "✗ containerd binary is not 1.7.28 (got: ${containerd_bin:-unknown})"
486+
success=false
487+
fi
461488

462489
# Check if Docker is running
463490
if $SUDO systemctl is-active --quiet docker; then
@@ -547,7 +574,7 @@ install_docker_version() {
547574

548575
# Unhold packages if they're held
549576
print_info "Unholding Docker packages..."
550-
$SUDO apt-mark unhold docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin 2>/dev/null || true
577+
$SUDO apt-mark unhold docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker-ce-rootless-extras 2>/dev/null || true
551578
echo ""
552579

553580
# Setup Docker repository if not already present
@@ -592,7 +619,8 @@ install_docker_version() {
592619
docker-ce-cli=$full_version \
593620
containerd.io \
594621
docker-buildx-plugin \
595-
docker-compose-plugin; then
622+
docker-compose-plugin \
623+
docker-ce-rootless-extras; then
596624
print_success "Docker $version installed successfully"
597625
else
598626
print_error "Failed to install Docker $version"

0 commit comments

Comments
 (0)