Skip to content

Instantly share code, notes, and snippets.

@maietta
Created February 14, 2025 04:17
Show Gist options
  • Save maietta/ee56cffaa77b6663345758869a72d96d to your computer and use it in GitHub Desktop.
Save maietta/ee56cffaa77b6663345758869a72d96d to your computer and use it in GitHub Desktop.
Self hosting SvelteKit SSR applications with Bun latest, with Health-checking.
ARG BUN_VERSION=latest
FROM oven/bun:${BUN_VERSION} AS builder
WORKDIR /app
ENV NODE_ENV=production
# Copy package files first
COPY --link bun.lock package.json ./
# Add conditional installation of svelte-adapter-bun and update config
RUN if ! grep -q "svelte-adapter-bun" package.json; then \
bun add -D svelte-adapter-bun; \
fi
# Install dependencies
RUN bun install --ci
# Copy remaining files
COPY --link . .
# Update svelte.config.js to use Bun adapter
RUN if grep -q "@sveltejs/adapter-" svelte.config.js; then \
sed -i 's/@sveltejs\/adapter-[a-zA-Z0-9-]\+/svelte-adapter-bun/g' svelte.config.js; \
fi
# Build the application
RUN bun --bun run vite build
# Production stage
FROM oven/bun:${BUN_VERSION}
WORKDIR /app
COPY --chown=bun:bun --from=builder /app /app
# Create healthcheck script inline
RUN echo 'const check = async () => {\
try {\
const response = await fetch(`http://127.0.0.1:${process.env.PORT}/`, {\
method: "HEAD"\
});\
process.exit(response.ok ? 0 : 1);\
} catch (err) {\
process.exit(1);\
}\
};\
check();' > /app/healthcheck.js
ENV PORT=3000
ENV PROTOCOL_HEADER=x-forwarded-proto
ENV HOST_HEADER=x-forwarded-host
EXPOSE 3000/tcp
USER bun
HEALTHCHECK --interval=10s --timeout=3s --start-period=2s --retries=3 \
CMD bun run /app/healthcheck.js
CMD ["bun", "--bun", "/app/build/index.js"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment