Photo by Gil Ndjouwou on Unsplash
Complete Guide to Deploying Next.js Standalone with Bun and Docker
Learn to deploy a Next.js standalone application using Bun and Docker as if you were deploying with Vercel. This guide includes a step-by-step walkthr
Why Deploy Next.js with Bun and Docker?
When Bun hit its stable version 1 and Vercel started supporting it as a package manager (though still relying on Node for runtime), I was intrigued. How could you simulate the Vercel deployment process locally? Specifically, how can you deploy a Next.js standalone app with Bun and Docker? This guide aims to answer these questions.
Prerequisites
Basic understanding of Docker, Next.js, and Bun
Node.js installed
Bun package manager installed
Step-by-Step Guide
Step 1: Setting Up Dockerfile
Here's the full dockerfile
you'll need.
FROM imbios/bun-node:18-slim AS deps
ARG DEBIAN_FRONTEND=noninteractive
# I use Asia/Jakarta as my timezone, you can change it to your timezone
RUN apt-get -y update && \
apt-get install -yq openssl git ca-certificates tzdata && \
ln -fs /usr/share/zoneinfo/Asia/Jakarta /etc/localtime && \
dpkg-reconfigure -f noninteractive tzdata
WORKDIR /app
# Install dependencies based on the preferred package manager
COPY package.json bun.lockb ./
RUN bun install --frozen-lockfile
# Build the app
FROM deps AS builder
WORKDIR /app
COPY . .
RUN bun run build
# Production image, copy all the files and run next
FROM node:18-slim AS runner
WORKDIR /app
ARG CONFIG_FILE
COPY $CONFIG_FILE /app/.env
ENV NODE_ENV production
# Uncomment the following line in case you want to disable telemetry during runtime.
# ENV NEXT_TELEMETRY_DISABLED 1
COPY --from=builder /app/.next/standalone ./
EXPOSE 3001
ENV PORT 3001
CMD ["node", "server.js"]
Step 2: Custom Shell Script
Create a custom shell script ./build.sh.
#!/bin/bash
set -e
# Run Next.js build with passed arguments
next build "$@"
# Copy files only if not in a CI environment
if [ -z "$CI" ]; then
cp -r ./public ./.next/standalone/public
cp -r ./.next/static ./.next/standalone/.next/static
fi
Common Errors and Troubleshooting
I ran into some issues during my first attempt, especially errors prefixed with node:. I fixed them by using imbios/bun-node
instead of oven/bun
as the base image. Because we need Node.js fallback for Bun's not yet implemented features.
Conclusion
By following this guide, you should have a Next.js app deployed, mimicking the Vercel deployment process, with Bun and Docker. Feel free to tweak and optimize the setup according to your project's needs.
What's Next?
Share this guide if you found it useful and leave a comment if you have any questions or improvements. Stay tuned for more!