Run a Bun app
In this guide we create and deploy a Bun app. To run this example, follow these steps:
-
Install the
kraft
CLI tool and a container runtime engine, e.g. Docker. -
Clone the
examples
repository andcd
into theexamples/bun
directory:
git clone https://github.com/kraftcloud/examplescd examples/bun/
Make sure to log into Unikraft Cloud by setting your token and a metro close to you.
We use fra0
(Frankfurt, 🇩🇪) in this guide:
# Set Unikraft Cloud access tokenexport UKC_TOKEN=token# Set metro to Frankfurt, DEexport UKC_METRO=fra0
When done, invoke the following command to deploy the application on Unikraft Cloud:
kraft cloud deploy -p 443:3000 -M 512 .
The output shows the instance URL and other details:
[●] 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: 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 Unikraft Cloud instance of the Bun instance:
curl https://quiet-pond-ao44imcg.fra0.kraft.host
Hello, World!
At any point in time, you can list information about the instance:
kraft cloud instance list
NAME FQDN STATE STATUS IMAGE MEMORY ARGS BOOT TIMEbun-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:
kraft cloud instance remove bun-700mp
Customize your Application
To customize the application, update the files in the repository, listed below:
Kraftfile
: the Unikraft Cloud specificationDockerfile
: the Docker-specified application filesystemserver.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"]
FROM oven/bun:1 AS bun
RUN set -xe; \mv /usr/local/bin/bun /usr/bin/bun; \mkdir /self; \ln -sfn /usr/bin/bun /self/exe
FROM alpine:3 AS sys
RUN set -xe; \mkdir -p /target/etc; \mkdir -p /blank; \apk --no-cache add \ ca-certificates \ tzdata \; \update-ca-certificates; \ln -sf /usr/share/zoneinfo/Etc/UTC /target/etc/localtime; \echo "Etc/UTC" > /target/etc/timezone;
FROM scratch
COPY --from=bun /self /proc/selfCOPY --from=bun /usr/bin/bun /usr/bin/bun
COPY --from=bun /lib/x86_64-linux-gnu/libc.so.6 /lib/x86_64-linux-gnu/libc.so.6COPY --from=bun /lib/x86_64-linux-gnu/libdl.so.2 /lib/x86_64-linux-gnu/libdl.so.2COPY --from=bun /lib/x86_64-linux-gnu/libm.so.6 /lib/x86_64-linux-gnu/libm.so.6COPY --from=bun /lib/x86_64-linux-gnu/libpthread.so.0 /lib/x86_64-linux-gnu/libpthread.so.0COPY --from=bun /lib64/ld-linux-x86-64.so.2 /lib64/ld-linux-x86-64.so.2
COPY --from=sys /target/etc /etcCOPY --from=sys /usr/share/zoneinfo/UTC /usr/share/zoneinfo/UTCCOPY --from=sys /usr/share/zoneinfo/Etc/UTC /usr/share/zoneinfo/Etc/UTCCOPY --from=sys /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crtCOPY --from=sys /blank /tmp
COPY ./server.ts /usr/src/server.ts
const port = process.env.PORT || 3000;
console.log(`Launching Bun HTTP server on port: ${port}, url: http://0.0.0.0:${port} 🚀`);
Bun.serve({port: port,fetch(_request) {return new Response("Hello, World!\n");},});
Lines in the Kraftfile
have the following roles:
-
spec: v0.6
: The currentKraftfile
specification version is0.6
. -
runtime: base-compat:latest
: The kernel to use. -
rootfs: ./Dockerfile
: Build the application root filesystem using theDockerfile
. -
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 thescratch
container image, to create a base image. -
COPY ...
: Copy required files to the application filesystem: thebun
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 theDockerfile
. -
If you want to replace
server.ts
with a different source file, update thecmd
line in theKraftfile
and replace/usr/src/server.ts
with the path to your new source file. -
More extensive changes may require extending the
Dockerfile
with additionalDockerfile
commands.
Learn More
Use the --help
option for detailed information on using Unikraft Cloud:
kraft cloud --help
Or visit the CLI Reference.