Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Bun docker nextjs


Bun docker nextjs


To containerize a Next.js application using Bun and Docker, follow these steps based on recent guides and examples:

Setting Up Your Next.js Project

1. Install Bun: You can install Bun using the following command:

bash
   curl -fsSL https://bun.sh/install | bash
   

2. Create a New Next.js Project: Use Bun to scaffold a new Next.js project:

bash
   bunx create-next-app my-app
   cd my-app
   

Dockerfile Configuration

You will need to create a Dockerfile in the root of your Next.js project. Below is an example Dockerfile that sets up the environment for running a Next.js application with Bun:

dockerfile
FROM oven/bun AS base

# Install dependencies only when needed
FROM base AS deps
WORKDIR /app
COPY package.json bun.lockb ./
RUN bun install --frozen-lockfile

# Build the app
FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
RUN bun run build

# Production image, copy all the files and run next
FROM base AS runner
WORKDIR /app
COPY --from=builder /app/.next/standalone ./
COPY public ./public

ENV NODE_ENV production
ENV PORT 3000

EXPOSE 3000

CMD ["bun", "server.js"]

Building and Running the Docker Container

1. Build the Docker Image: Run the following command in your terminal:

bash
   docker build -t my-nextjs-app .
   

2. Run the Docker Container: Once the image is built, you can run it using:

bash
   docker run -p 3000:3000 my-nextjs-app
   

Accessing Your Application

After running the container, your Next.js application should be accessible at `https://localhost:3000`.

Additional Resources

- For more details on troubleshooting and optimizing your setup, refer to guides that discuss common issues when deploying Next.js with Bun and Docker, such as build errors or runtime issues[1][5][6].

This setup allows you to leverage Bun's performance benefits while maintaining compatibility with Next.js features in a containerized environment.

Citations:
[1] https://www.angelospanag.me/blog/containerising-nextjs-using-docker-and-bun
[2] https://github.com/nbeck415/bun-nextjs-dockerized
[3] https://app.daily.dev/posts/complete-guide-to-deploying-next-js-standalone-with-bun-and-docker-co8fzglyn
[4] https://github.com/oven-sh/bun/issues/4795
[5] https://dev.to/imamdev_/complete-guide-to-deploying-nextjs-standalone-with-bun-and-docker-1fc9
[6] https://nextjs.org/docs/app/building-your-application/deploying
[7] https://bun.sh/guides/ecosystem/nextjs
[8] https://github.com/oven-sh/bun/discussions/1040