From 7a15625c6bc67bed21fb5be1ebe17b6c65445cc8 Mon Sep 17 00:00:00 2001 From: Fallen_Breath Date: Tue, 30 Jul 2024 15:09:07 +0800 Subject: [PATCH] persist ISR cache between containers --- Dockerfile | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index a14a0e1..a271ee2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -26,4 +26,48 @@ COPY --from=builder /app/public ./public COPY --from=builder /app/.next/standalone ./ COPY --from=builder /app/.next/static ./.next/static -CMD ["node", "server.js"] +RUN < entrypoint.sh +#!/bin/sh +set -e +dst=/app/.next +src=/data/.next +dst_id_file="$dst/BUILD_ID" +src_id_file="$src/BUILD_ID" + +img_build_id="$(cat "$src_id_file")" +if [ -f "$dst_id_file" ]; then + old_build_id="$(cat "$dst_id_file")" +else + old_build_id="" +fi + +if [ "$img_build_id" != "$old_build_id" ]; then + echo "[INIT] Build ID changed, recreating .next files" + echo "[INIT] Existing build ID at $dst_id_file: $old_build_id" + echo "[INIT] Image build ID: $img_build_id" + + if [ -d "$dst" ]; then + rm -rf "$dst/*" + cp -rp "$src/." "$dst" + else + echo "[INIT] runtime .next dir $dst does not exist, no volume mount? use symlink instead" + ln -s "$src" "$dst" + fi +else + echo "[INIT] Build ID unchanged, reusing existing .next files" + echo "[INIT] Build ID: $img_build_id" +fi + +exec node server.js +EOF + +chmod +x entrypoint.sh +EOT + +CMD ["sh", "entrypoint.sh"]