SvelteKit
This guide explains how to create and deploy a SvelteKit app. SvelteKit builds on Svelte, a UI framework that uses a compiler to let you write breathtakingly concise components that do minimal work in the browser.
To run this example, follow these steps:
-
Install the
kraftCLI tool and a container runtime engine, for example Docker. -
Clone the
examplesrepository andcdinto theexamples/node21-sveltekit/directory:
Code(bash)
Make sure to log into Unikraft Cloud by setting your token and a metro close to you.
This guide uses fra (Frankfurt, ๐ฉ๐ช):
Code(bash)
When done, invoke the following command to deploy the app on Unikraft Cloud:
Code(bash)
The output shows the instance address and other details:
Code(ansi)
In this case, the instance name is node21-sveltekit-zmt39 and the address is https://dark-fog-z18n0ej1.fra.unikraft.app.
They're different for each run.
Use curl to query the Unikraft Cloud instance:
Code(bash)
Code(text)
Or even better, point a browser at it ๐ This will get you to play with the SvelteKit demo app.
You can list information about the instance by running:
Code(bash)
Code(text)
When done, you can remove the instance:
Code(bash)
Customize your app
To customize the app, update the files in the repository.
That is, you update the SvelteKit / Node / npm-specific files, or the Unikraft Cloud-specific files.
This section details each below.
Updating the app
Updating the SvelteKit app is reliant on making npm-related updates.
npm is a package manager for Node.
It's used to install dependencies for Node apps.
npm uses a package.json file to list required dependencies (with versions).
npm create svelte@latest generated the app files, using the @sveltejs/adapter-node adapter.
The src/routes/sverdle/ directory contains the core implementation.
Detailed information on updating the implementation is part of the official SvelteKit documentation.
Updates to the implementation will probably require updates to the package.json file.
The package.json file lists npm app dependencies.
Deploying locally
Before deploying the SvelteKit app on Unikraft Cloud, you can deploy locally.
Assuming npm is installed, use the following commands:
Code(bash)
Code(text)
You can test the app locally by pointing your browser to http://localhost:5173/.
Sample specification
Lines in the Kraftfile have the following roles:
-
spec: v0.6: The currentKraftfilespecification version is0.6. -
runtime: node:21: The Unikraft runtime kernel to use is Node 21. -
rootfs: ./Dockerfile: Build the app root filesystem using theDockerfile. -
cmd: [["/usr/bin/node", "/app/build/index.js"]]: Use/usr/bin/node /app/build/index.jsas the starting command of the instance.
Lines in the Dockerfile have the following roles:
-
FROM node:21-alpine AS deps: Use the base image of thenode:21-alpinecontainer. This provides thenpmbinary and other Node-related components. Name the current imagedeps. -
WORKDIR /app: Use/appas working directory. All other commands in theDockerfilerun inside this directory. -
COPY package.json /app: Copy thenpmconfiguration file to be able to install dependencies. -
RUN npm install: Installnpmcomponents listed inpackages.json. -
FROM deps AS build: Create a fresh checkpoint of the base image for the build phase. -
COPY . /app: Copy contents of the current directory (the actual app files) in the Docker filesystem. Note that paths in the.dockerignorefile aren't copied. -
RUN npm run build; ...: Build the files required for running the app standalone. The app entry point is the resulting/app/build/index.jsfile that's used as the start command in theKraftfile. To run the app, configure it as a module. -
FROM scratch as prod: Use thescratchempty container` as the actual runtime environment. It will only contain the required files from the build. -
COPY --from=build ...: Copy existing files from newbuildimage to thescratch-based image. This means the app build directory (/app/build) and the required dependencies (/app/node_modules).
Customization options
It's unlikely you will have to update the Kraftfile.
The only potential update is the cmd option.
But since SvelteKit / npm generates the /app/build/index.js file, you're unlikely to update it.
You also don't need to change the Dockerfile.
Updates to the app would be part of the package.json file or the src/ directory.
And updating these won't affect the contents of the Dockerfile.
Still, if required, apps may require extending the Dockerfile with extra Dockerfile commands.
Learn more
Use the --help option for detailed information on using Unikraft Cloud:
Code(bash)
Or visit the CLI Reference.