Lesson 8 of 9 · 24m
Shipping Bun in Containers and Server Processes
Build reproducible Bun images, handle caching and signals, choose single-file or node_modules deployment, and operate services gracefully.
The moment Bun reaches production, the question stops being whether it is clever. It becomes whether the process starts predictably, shuts down cleanly, ships only what it needs, and gives operators enough evidence when something is wrong. Containers make these questions sharper because every missing file and every signal mistake becomes a deployment incident.
Start with a boring Dockerfile
FROM oven/bun:1.2.21 AS deps
WORKDIR /app
COPY package.json bun.lock ./
RUN bun install --frozen-lockfile
FROM oven/bun:1.2.21 AS build
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
RUN bun run typecheck && bun test && bun build src/server.ts --outdir dist --target bun --sourcemap=external
FROM oven/bun:1.2.21-slim
WORKDIR /app
ENV NODE_ENV=production
COPY --from=build /app/dist ./dist
COPY --from=build /app/package.json ./package.json
CMD ["bun", "dist/server.js"]Adjust for your real version, workspace layout, and asset needs. The important properties are pinned base image, frozen dependencies, verification before artifact creation, and a small runtime stage.
Decide what the artifact contains
There are three common deployment shapes:
source + node_modules easiest for dynamic dependencies, largest image
bundled JS + assets smaller, explicit static dependency graph
compiled single binary attractive distribution, more build constraintsbun build --compile can produce a standalone executable, but that does not remove operating-system portability, asset embedding, or native dependency concerns. Choose the shape because it fits the operational model, not because it feels elegant.
Signals are part of the application
A container stop sends a signal before it kills the process. Your server should close listeners, finish in-flight work where possible, close database pools, and exit:
const server = Bun.serve({
port: 3000,
fetch: handleRequest,
});
async function shutdown(signal: string) {
console.log(`received ${signal}; draining`);
server.stop();
await db.close();
process.exit(0);
}
process.on("SIGTERM", () => void shutdown("SIGTERM"));
process.on("SIGINT", () => void shutdown("SIGINT"));If you run behind an orchestrator, test the stop behavior. A health endpoint that stays green while the process ignores SIGTERM is not resilience; it is delayed failure.
Configuration belongs outside the bundle
Use environment variables or mounted configuration for deployment-specific values:
const port = Number(process.env.PORT ?? 3000);
const databaseUrl = process.env.DATABASE_URL;
if (!databaseUrl) throw new Error("DATABASE_URL is required");Fail fast on missing required configuration. Do not bake secrets into layers, sourcemaps, or client-visible bundles.
Observability does not come for free
Bun can serve HTTP quickly, but you still need request logs, tracing context, metrics, crash output, and error serialization. If you replace a mature Node service wrapper, compare operational features as carefully as syntax compatibility.
Field notes
- Pin image tags.
latestturns every deployment into a runtime migration experiment. - Do not copy
node_modulesblindly if the artifact is bundled; understand which runtime imports remain. - Test SIGTERM behavior in the actual container. Shell-local Ctrl+C is not equivalent.
- Slim images reduce attack surface, but verify fonts, CA certificates, timezone data, and native libraries your dependencies expect.
Was this lesson helpful?