Yes, you can use Bun for both development and production in a Docker setup. Here's how you can approach it:
Development Setup
1. Use the official Bun Docker image as the base for your development environment. For example:
dockerfile
FROM oven/bun:1
2. Mount your project directory into the container's working directory so changes are reflected immediately. For example:
dockerfile
WORKDIR /app
COPY . .
3. Use Bun's hot module replacement (HMR) for fast development iterations. Update your `package.json` start script:
json
"scripts": {
"start": "bun run --hot index.ts"
}
4. Run the container in interactive mode to get a shell inside the container:
bash
docker run -it -v $(pwd):/app -p 3000:3000 bun-dev
Production Setup
1. Use multi-stage builds to create a production-ready image:
dockerfile
# Development stage
FROM oven/bun:1 AS dev
WORKDIR /app
COPY . .
RUN bun install
# Production stage
FROM oven/bun:1 AS prod
WORKDIR /app
COPY --from=dev /app .
RUN bun prune --production
2. Copy only production dependencies and built artifacts to the final image.
3. Set the start command to run the production server:
dockerfile
CMD ["bun", "run", "index.ts"]
4. Build and run the production image:
bash
docker build -t bun-prod .
docker run -d -p 3000:3000 bun-prod
By using multi-stage builds, you can leverage Bun for both development and production while keeping the final image slim and optimized for production.
The key points are:
1. Use the official Bun Docker image as the base
2. Mount the project directory for development
3. Use Bun's HMR for fast iterations
4. Use multi-stage builds to create a production image
5. Copy only production dependencies and built artifacts
6. Set the start command to run the production server
This setup allows you to develop with Bun's fast feedback loop while still deploying a production-ready Docker image built with Bun[1][2][3][4].
Citations:[1] https://dev.to/code42cate/how-to-dockerize-a-bun-app-38e4
[2] https://www.youtube.com/watch?v=Fd96devS1yg
[3] https://andrekoenig.de/articles/using-bun-as-the-package-manager-in-production-ready-docker-images
[4] https://stackoverflow.com/questions/77110962/bun-not-found-in-docker-trying-to-do-split-base-build-image
[5] https://github.com/oven-sh/bun/discussions/1040
[6] https://www.telerik.com/blogs/getting-started-bun-react-developers
[7] https://github.com/oven-sh/bun/discussions/559
[8] https://bun.sh/guides/ecosystem/docker