Node
This guide explains how to create and deploy a simple Node-based HTTP web server. To run this example, follow these steps:
-
Install the
kraftCLI tool and a container runtime engine, for example Docker. -
Clone the
examplesrepository andcdinto theexamples/http-node21/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 http-node21-ubl8g and the address is https://ancient-haze-sd3wwi0x.fra.unikraft.app.
They're different for each run.
Use curl to query the Unikraft Cloud instance of the Node-based HTTP web server:
Code(bash)
Code(text)
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, listed below:
server.js: the actual Node HTTP serverKraftfile: the Unikraft Cloud specificationDockerfile: the Docker-specified app filesystem
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", "/usr/src/server.js"]: Use/usr/bin/node /usr/src/server.jsas the starting command of the instance.
Lines in the Dockerfile have the following roles:
-
FROM scratch: Build the filesystem from thescratchcontainer image, to create a base image. -
COPY ./server.js /usr/src/server.js: Copy the server implementation file (server.js) in the Docker filesystem (in/usr/src/server.js).
The following options are available for customizing the app:
-
If you only update the implementation in the
server.jssource 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
COPYcommand in theDockerfile. -
If you want to replace
server.jswith a different source file, update thecmdline in theKraftfileand replace/usr/src/server.jswith the path to your new source file. -
More extensive changes may require extending the
Dockerfile(seeDockerfilesyntax reference). This includes the use of Node frameworks and the use ofnpm, as shown in the next section.
Using npm
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).
The node21-expressjs example in the examples repository details the use of npm to deploy an app using the ExpressJS framework on Unikraft Cloud.
Clone the examples repository and cd into the node21-expressjs directory:
Code(bash)
Run the command below to deploy the app on Unikraft Cloud:
Code(bash)
Differences from the http-node21 app are also the steps required to create an npm-based app:
-
Add the
package.jsonfile used bynpm. -
Add framework-specific source files. In this case, this means
app/index.js. -
Update the
Dockerfileto:-
COPYthe local files. -
RUNthenpm installcommand to install dependencies. -
COPYof the resulting and required files (node_modules/andapp/index.js) in the app filesystem, using thescratchcontainer.
-
The following lists the files:
The package.json file lists the express dependency.
The Kraftfile is the same one used for http-node21.
For Dockerfile newly added lines have the following roles:
-
FROM node:21-alpine AS build: Use the base image of thenode:21-alpinecontainer. This provides thenpmbinary and other Node-related components. Name the current imagebuild. -
WORKDIR /usr/src: Use/usr/srcas working directory. All other commands in theDockerfilerun inside this directory. -
COPY . /usr/src/: Copy the contents of the local current directory to the Docker filesystem. Note that paths in the.dockerignorefile aren't copied. This means thatpackage.jsonandapp/index.jsare copied. -
RUN npm install: Installnpmcomponents listed inpackages.json. -
COPY --from=build ...: Copy existing files in the newbuildimage in thescratch-based image./etc/os-releasemust copy to provide the distribution information required by node./usr/src/node_modulesare thenpm-generated files./usrc/src/app/index.jsis the originalExpressJSsource code file.
Similar actions apply to other npm-based apps.
See also other Node examples: node18-prisma-rest-express and node21-nextjs.
Learn more
Use the --help option for detailed information on using Unikraft Cloud:
Code(bash)
Or visit the CLI Reference.