Zudoku
Guides

Bun

This guide explains how to create and deploy a Bun app. To run this example, follow these steps:

  1. Install the kraft CLI tool and a container runtime engine, for example Docker.

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

Code(bash)
git clone https://github.com/kraftcloud/examples cd examples/bun/

Make sure to log into Unikraft Cloud by setting your token and a metro close to you. This guide uses fra (Frankfurt, ๐Ÿ‡ฉ๐Ÿ‡ช):

Code(bash)
# Set Unikraft Cloud access token export UKC_TOKEN=token # Set metro to Frankfurt, DE export UKC_METRO=fra

When done, invoke the following command to deploy the app on Unikraft Cloud:

Code(bash)
kraft cloud deploy -p 443:3000 -M 512 .

The output shows the instance address and other details:

Code(ansi)
[โ—] Deployed successfully! โ”‚ โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ name: bun-700mp โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ uuid: e467a880-075c-41e0-97ac-88e3e938523e โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ state: starting โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ domain: https://quiet-pond-ao44imcg.fra.unikraft.app โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ image: bun@sha256:dfcbee1efe0d8a1d43ab2dab70cf1cc5066bb1353aa1c528c745533d2cc33276 โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ memory: 512 MiB โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€ service: 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 address is https://quiet-pond-ao44imcg.fra.unikraft.app. They're different for each run.

Use curl to query the Unikraft Cloud instance of the Bun instance:

Code(bash)
curl https://quiet-pond-ao44imcg.fra.unikraft.app
Code(text)
Hello, World!

You can list information about the instance by running:

Code(bash)
kraft cloud instance list
Code(text)
NAME FQDN STATE STATUS IMAGE MEMORY ARGS BOOT TIME bun-700mp quiet-pond-ao44imcg.fra.unikraft.app 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:

Code(bash)
kraft cloud instance remove bun-700mp

Customize your app

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

  • Kraftfile: the Unikraft Cloud specification
  • Dockerfile: the Docker-specified app filesystem
  • server.ts: the Bun server implementation
Code(yaml)
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 app 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 app 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 app:

  • If you only update the implementation in the server.ts source file, you don't need to make any other changes.

  • If you want to add extra 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 (see Dockerfile syntax reference).

Learn more

Use the --help option for detailed information on using Unikraft Cloud:

Code(bash)
kraft cloud --help

Or visit the CLI Reference.

Last modified on