Skip to content
当前有符合你浏览器所设置语言的版本,是否前往zh-CN版本的网站?
There is a version suitable for your browser's language settings. Would you like to go to the zh-CN language of the site?
HomeDocument

Bun

H1

Bun is the default runtime environment for Milkio. As a fast JavaScript runtime, it is compatible with Node.js and supports running TypeScript code directly.

Getting Started

When creating an application, choose bun as the runtime environment.

Terminal window
bun create milkio

Deployment Guide

You can deploy your application directly on a server, but we recommend using Kubernetes or some container-supported Serverless platforms. Examples include Google Cloud Run, AWS Lambda, etc.

Alternatively, you can explore Serverless platforms with custom runtimes like Cloudflare Workers or Vercel without using Bun. These platforms offer simpler deployment experiences compared to Kubernetes or container solutions. However, they often come with limitations in compatibility, such as not being able to utilize most Node.js or Bun functionalities.

Packaging

You can use the bun command to package your application. Although Bun is designed as a JavaScript runtime, it maintains good compatibility with Node.js. Therefore, similar to using Node.js, it will execute the scripts in your /package.json.

Terminal window
bun run build

This command will automatically call the build script defined in the scripts section of your /package.json. By default, the command looks like this:

Terminal window
bun build ./run-serve.ts --splitting --sourcemap=inline --outdir=./dist --target=bun --minify

After execution, a /dist folder will be created in your project directory containing compressed and optimized code. Deploy all files in this directory to the server, then run the following command to start your application:

Terminal window
bun run run-serve.js

Deployment Using Docker

Docker is a virtual container. By writing a Dockerfile, we can package the code and environment into an image and run the image as a virtual container at any time.

# ------ Stage 0: Build, migrate database, package executable ------
FROM oven/bun:debian as builder
ENV NODE_VERSION 22.1.0
WORKDIR /root/workspace/
COPY ./package.json .
COPY ./bun.lockb .
RUN chmod -R u=rwx . && bun i --no-cache
COPY . .
# Remove environment variables used for local development
RUN rm -f .env \
# Generate
&& bun run milkio gen \
# Package the executable
&& bun run build
# ------ Stage 1: Convert artifacts into final image ------
FROM oven/bun:slim as prod
WORKDIR /root/workspace/
COPY --from=0 /root/workspace/dist/* ./
RUN chmod -R u=rwx . /root/workspace/
CMD ["bun", "run", "run-serve.js"]

This Dockerfile has been carefully optimized. Installing dependencies first and then copying the code can cache the dependency part, speeding up the image build process each time.