From 14c065f57d0485afe4e7eb924d5e543c027a1b6d Mon Sep 17 00:00:00 2001 From: Subramanya Chakravarthy Date: Thu, 7 Aug 2025 15:08:02 +0530 Subject: [PATCH 1/2] feat(docker): update Dockerfile for Alpine base and add healthcheck script --- Dockerfile | 25 ++++++++++++++++++------- healthcheck.js | 13 +++++++++++++ 2 files changed, 31 insertions(+), 7 deletions(-) create mode 100644 healthcheck.js diff --git a/Dockerfile b/Dockerfile index 376565c..2970dcb 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,16 +1,18 @@ -FROM oven/bun AS build +FROM oven/bun:alpine AS build WORKDIR /app # Cache packages installation -COPY package.json package.json -COPY bun.lock bun.lock +COPY package.json bun.lock ./ -RUN bun install +# Install dependencies +RUN bun install --frozen-lockfile +# Copy source files COPY ./src ./src COPY ./prisma ./prisma -COPY ./tsconfig.json tsconfig.json +COPY ./tsconfig.json ./ +COPY ./healthcheck.js ./ ENV NODE_ENV=production @@ -29,10 +31,19 @@ FROM gcr.io/distroless/base WORKDIR /app +# Copy the compiled binary and healthcheck COPY --from=build /app/server server +COPY --from=build /app/healthcheck.js healthcheck.js + +# Ensure the binary is executable +USER nonroot:nonroot ENV NODE_ENV=production -CMD ["./server"] +# Health check +HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \ + CMD ["/usr/local/bin/bun", "/app/healthcheck.js"] + +EXPOSE 3000 -EXPOSE 3000 \ No newline at end of file +CMD ["./server"] \ No newline at end of file diff --git a/healthcheck.js b/healthcheck.js new file mode 100644 index 0000000..a7b3e38 --- /dev/null +++ b/healthcheck.js @@ -0,0 +1,13 @@ +// healthcheck.js +const url = "http://localhost:3000/health"; + +try { + const res = await fetch(url, { timeout: 2000 }); + if (res.ok) { + process.exit(0); // Healthy + } else { + process.exit(1); // HTTP error + } +} catch (err) { + process.exit(1); // Network error or timeout +} From d636756c0dd925429782a5cd4e9ad7328eda83e4 Mon Sep 17 00:00:00 2001 From: Subramanya Chakravarthy Date: Thu, 7 Aug 2025 15:46:53 +0530 Subject: [PATCH 2/2] refactor(docker): remove healthcheck.js and implement health check using curl --- Dockerfile | 12 +++++++----- healthcheck.js | 13 ------------- 2 files changed, 7 insertions(+), 18 deletions(-) delete mode 100644 healthcheck.js diff --git a/Dockerfile b/Dockerfile index 2970dcb..94315cb 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,7 +12,6 @@ RUN bun install --frozen-lockfile COPY ./src ./src COPY ./prisma ./prisma COPY ./tsconfig.json ./ -COPY ./healthcheck.js ./ ENV NODE_ENV=production @@ -27,13 +26,16 @@ RUN bun build \ --outfile server \ ./src/index.ts +FROM alpine AS curl-downloader +RUN apk add --no-cache curl + FROM gcr.io/distroless/base WORKDIR /app -# Copy the compiled binary and healthcheck +# Copy the compiled binaries COPY --from=build /app/server server -COPY --from=build /app/healthcheck.js healthcheck.js +COPY --from=curl-downloader /usr/bin/curl /usr/bin/curl # Ensure the binary is executable USER nonroot:nonroot @@ -41,8 +43,8 @@ USER nonroot:nonroot ENV NODE_ENV=production # Health check -HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \ - CMD ["/usr/local/bin/bun", "/app/healthcheck.js"] +HEALTHCHECK --interval=30s --timeout=5s --start-period=5s --retries=3 \ + CMD ["/usr/bin/curl", "-f", "http://localhost:3000/health"] EXPOSE 3000 diff --git a/healthcheck.js b/healthcheck.js deleted file mode 100644 index a7b3e38..0000000 --- a/healthcheck.js +++ /dev/null @@ -1,13 +0,0 @@ -// healthcheck.js -const url = "http://localhost:3000/health"; - -try { - const res = await fetch(url, { timeout: 2000 }); - if (res.ok) { - process.exit(0); // Healthy - } else { - process.exit(1); // HTTP error - } -} catch (err) { - process.exit(1); // Network error or timeout -}