Skip to content

Commit f0efe88

Browse files
committed
fix(docker): harden bun install retry cache handling
1 parent f5d595b commit f0efe88

3 files changed

Lines changed: 30 additions & 3 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,7 @@ just run start --full-rebuild-sandbox-image --local-opencode-submodule
459459
460460
For new Docker build steps, follow this checklist:
461461
- Prefer BuildKit cache mounts (`RUN --mount=type=cache`) for package caches (`apt`, `bun`, `cargo`, `pip`, and `pnpm/npm`).
462+
- For `bun install` in container builds, use a dedicated install-cache mount plus a short retry loop that clears that cache between attempts to recover from occasional corrupted/interrupted cache artifacts.
462463
- Create and remove temporary workdirs in the same `RUN` layer (for example `/tmp/opencode-repo`).
463464
- Do not defer cleanup to later layers; deleted files still exist in lower layers.
464465
- Keep builder-stage artifacts out of runtime layers by copying only final outputs.

packages/core/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,7 @@ just run start --full-rebuild-sandbox-image --local-opencode-submodule
459459
460460
For new Docker build steps, follow this checklist:
461461
- Prefer BuildKit cache mounts (`RUN --mount=type=cache`) for package caches (`apt`, `bun`, `cargo`, `pip`, and `pnpm/npm`).
462+
- For `bun install` in container builds, use a dedicated install-cache mount plus a short retry loop that clears that cache between attempts to recover from occasional corrupted/interrupted cache artifacts.
462463
- Create and remove temporary workdirs in the same `RUN` layer (for example `/tmp/opencode-repo`).
463464
- Do not defer cleanup to later layers; deleted files still exist in lower layers.
464465
- Keep builder-stage artifacts out of runtime layers by copying only final outputs.

packages/core/src/docker/Dockerfile

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,16 @@ COPY --chown=opencoder:opencoder packages/opencode /tmp/opencode-local
528528
# - Prefer BuildKit cache mounts for bun/cargo cache paths if this block is
529529
# expanded; avoid persisting package caches in committed layers.
530530
# - Keep explicit cleanup as a defensive fallback even when cache mounts are used.
531-
RUN OPENCODE_COMMIT_OVERRIDE="${OPENCODE_COMMIT:-}" \
531+
# Reliability note for Bun dependency install:
532+
# - We use a dedicated Bun install cache mount so BuildKit can reuse downloaded
533+
# packages across builds without polluting image layers.
534+
# - In CI/container builds, Bun's cached install artifacts can occasionally
535+
# become inconsistent (for example after interrupted network/download steps),
536+
# which causes `bun install --frozen-lockfile` to fail nondeterministically.
537+
# - The retry loop intentionally clears only this cache dir between attempts so
538+
# each retry gets a clean cache state while preserving reproducibility.
539+
RUN --mount=type=cache,target=/home/opencoder/.bun/install/cache,uid=1000,gid=1000,mode=0755 \
540+
OPENCODE_COMMIT_OVERRIDE="${OPENCODE_COMMIT:-}" \
532541
&& OPENCODE_LOCAL_REF="${OPENCODE_LOCAL_REF:-local-unknown}" \
533542
&& OPENCODE_COMMIT="0c796c56e7234afc9547b9d341ba1e92eeee3f54" \
534543
&& if [ -n "${OPENCODE_COMMIT_OVERRIDE}" ]; then OPENCODE_COMMIT="${OPENCODE_COMMIT_OVERRIDE}"; fi \
@@ -547,7 +556,22 @@ RUN OPENCODE_COMMIT_OVERRIDE="${OPENCODE_COMMIT:-}" \
547556
git checkout "${OPENCODE_COMMIT}"; \
548557
fi \
549558
&& cd /tmp/opencode-repo \
550-
&& bun install --frozen-lockfile \
559+
&& sudo mkdir -p /home/opencoder/.bun/install/cache \
560+
&& sudo chown -R opencoder:opencoder /home/opencoder/.bun/install/cache \
561+
&& export BUN_INSTALL_CACHE_DIR=/home/opencoder/.bun/install/cache \
562+
&& bun_install_ok=0; \
563+
for attempt in 1 2 3; do \
564+
if bun install --frozen-lockfile; then \
565+
bun_install_ok=1; \
566+
break; \
567+
fi; \
568+
echo "bun install failed (attempt ${attempt}); clearing cache and retrying..." >&2; \
569+
sudo find /home/opencoder/.bun/install/cache -mindepth 1 -maxdepth 1 -exec rm -rf {} + || true; \
570+
done; \
571+
if [ "${bun_install_ok}" -ne 1 ]; then \
572+
echo "bun install failed after 3 attempts." >&2; \
573+
exit 1; \
574+
fi \
551575
&& cd packages/opencode \
552576
&& if [ "${OPENCODE_SOURCE}" = "local" ]; then \
553577
# Local mode omits .git from context for performance and safety, so set
@@ -574,7 +598,8 @@ RUN OPENCODE_COMMIT_OVERRIDE="${OPENCODE_COMMIT:-}" \
574598
&& sudo cp target/release/opencode-broker /usr/local/bin/opencode-broker \
575599
&& sudo chmod 4755 /usr/local/bin/opencode-broker \
576600
&& rm -rf /tmp/opencode-repo /tmp/opencode-local \
577-
&& rm -rf /home/opencoder/.bun/install/cache /home/opencoder/.bun/cache /home/opencoder/.cache/bun
601+
&& sudo find /home/opencoder/.bun/install/cache -mindepth 1 -maxdepth 1 -exec rm -rf {} + || true \
602+
&& sudo rm -rf /home/opencoder/.bun/cache /home/opencoder/.cache/bun
578603

579604
# -----------------------------------------------------------------------------
580605
# Stage 3: Runtime

0 commit comments

Comments
 (0)