Skip to content

Run a Bun app

In this guide we create and deploy a Bun app. To run this example, follow these steps:

  1. Install the kraft CLI tool and a container runtime engine, e.g. Docker.

  2. Clone the examples repository and cd into the examples/bun directory:

Terminal window
git clone https://github.com/kraftcloud/examples
cd examples/bun/

Make sure to log into KraftCloud by setting your token and a metro close to you. We use fra0 (Frankfurt, 🇩🇪) in this guide:

Terminal window
# Set KraftCloud access token
export KRAFTCLOUD_TOKEN=token
# Set metro to Frankfurt, DE
export KRAFTCLOUD_METRO=fra0

When done, invoke the following command to deploy the application on KraftCloud:

Terminal window
kraft cloud deploy -p 443:3000 -M 512 .

The output shows the instance URL and other details:

Terminal window
[] Deployed successfully!
────────── name: bun-700mp
────────── uuid: e467a880-075c-41e0-97ac-88e3e938523e
───────── state: starting
─────────── domain: https://quiet-pond-ao44imcg.fra0.kraft.host
───────── image: bun@sha256:dfcbee1efe0d8a1d43ab2dab70cf1cc5066bb1353aa1c528c745533d2cc33276
──────── memory: 512 MiB
service group: quiet-pond-ao44imcg
── private fqdn: bun-700mp.internal
──── private ip: 172.16.3.3
────────── args: /usr/bin/bun run /usr/src/server.ts

├────────── name: bun-700mp ├────────── uuid: e467a880-075c-41e0-97ac-88e3e938523e ├───────── state: starting ├──────── domain: https://quiet-pond-ao44imcg.fra0.kraft.host ├───────── image: razvan.unikraft.io/bun@sha256

├──────── memory: 512 MiB ├─ service group: quiet-pond-ao44imcg ├── private fqdn: bun-700mp.internal ├──── private ip: 172.16.3.3 └────────── args: /usr/bin/bun run /usr/src/server.ts

In this case, the instance name is bun-700mp and the URL is https://quiet-pond-ao44imcg.fra0.kraft.host. They are different for each run.

Use curl to query the KraftCloud instance of the Bun instance:

Terminal window
curl https://quiet-pond-ao44imcg.fra0.kraft.host
Hello, World!

At any point in time, you can list information about the instance:

Terminal window
kraft cloud instance list
NAME FQDN STATE STATUS IMAGE MEMORY ARGS BOOT TIME
bun-700mp quiet-pond-ao44imcg.fra0.kraft.host running since 3mins bun@sha256:dfcbee1efe0d8a1d43ab... 512 MiB /usr/bin/bun run /usr/src/server.ts 289.03 ms

When done, you can remove the instance:

Terminal window
kraft cloud instance remove bun-700mp

Customize your Application

To customize the application, update the files in the repository, listed below:

  • Kraftfile: the KraftCloud specification
  • Dockerfile: the Docker-specified application filesystem
  • server.ts: the Bun server implementation
spec: v0.6
name: bun
runtime: base-compat:latest
rootfs: ./Dockerfile
cmd: ["/usr/bin/bun", "run", "/usr/src/server.ts"]

Lines in the Kraftfile have the following roles:

  • spec: v0.6: The current Kraftfile specification version is 0.6.

  • runtime: base-compat:latest: The kernel to use.

  • rootfs: ./Dockerfile: Build the application root filesystem using the Dockerfile.

  • cmd: ["/usr/bin/bun", "run", "/usr/src/server.ts"]: Use /usr/bin/bun run /usr/src/server.ts as the starting command of the instance.

Lines in the Dockerfile have the following roles:

  • FROM scratch: Build the filesystem from the scratch container image, to create a base image.

  • COPY ...: Copy required files to the application filesystem: the bun binary executable, libraries, configuration files, the /usr/src/server.ts implementation.

  • RUN ...: Run specific commands to generate or to prepare the filesystem contents.

The following options are available for customizing the application:

  • If only updating the implementation in the server.ts source file, no other change is required.

  • If you want to add additional files, you need to copy them into the filesystem using the COPY command in the Dockerfile.

  • If you want to replace server.ts with a different source file, update the cmd line in the Kraftfile and replace /usr/src/server.ts with the path to your new source file.

  • More extensive changes may require extending the Dockerfile with additional Dockerfile commands.

Learn More

Use the --help option for detailed information on using KraftCloud:

Terminal window
kraft cloud --help

Or visit the CLI Reference.