Run a Node app
In this guide we create and deploy a simple Node-based HTTP web server. To run this example, follow these steps:
kraftCLI tool and a container runtime engine, e.g. Docker.
Make sure to log into KraftCloud by setting your token and a metro close to you.
fra0 (Frankfurt, 🇩🇪) in this guide:
When done, invoke the following command to deploy the application on KraftCloud:
The output shows the instance URL and other details:
In this case, the instance name is
http-node21-ubl8g and the URL is
They are different for each run.
curl to query the KraftCloud instance of the Node-based HTTP web server:
At any point in time, you can list information about the instance:
When done, you can remove the instance:
Customize your Application
To customize the application, update the files in the repository, listed below:
server.js: the actual Node HTTP server
Kraftfile: the KraftCloud specification
Dockerfile: the Docker-specified application filesystem
Lines in the
Kraftfile have the following roles:
spec: v0.6: The current
Kraftfilespecification version is
runtime: node:21: The Unikraft runtime kernel to use is Node 21.
rootfs: ./Dockerfile: Build the application root filesystem using the
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:
COPY ./server.js /usr/src/server.js: Copy the server implementation file (
server.js) in the Docker filesystem (in
The following options are available for customizing the application:
If only updating the implementation in the
server.jssource file, no other change is required.
If you want to add additional files, you need to copy them into the filesystem using the
COPYcommand in the
If you want to replace
server.jswith a different source file, update the
cmdline in the
/usr/src/server.jswith the path to your new source file.
npm is a package manager for Node.
It is used to install dependencies for Node applications.
npm uses a
package.json file to list required dependencies (with versions).
node21-expressjs example in the
examples repository details the use of
npm to deploy an application using the ExpressJS framework on KraftCloud.
examples repository and
cd into the
Run the command below to deploy the application on KraftCloud:
Differences from the
http-node21 app are also the steps required to create an
package.jsonfile used by
Add framework-specific source files. In our case, this means
COPYthe local files.
npm installcommand to install dependencies.
COPYof the resulting and required files (
app/index.js) in the application filesystem, using the
The files are listed below:
package.json file lists the
Kraftfile is the same one used for
Dockerfile newly added lines have the following roles:
FROM node:21-alpine AS build: Use the base image of the
node:21-alpinecontainer. This provides the
npmbinary and other Node-related components. Name the current image
WORKDIR /usr/src: Use
/usr/srcas working directory. All other commands in the
Dockerfilerun inside this directory.
COPY . /usr/src/: Copy the contents of the local current directory to the Docker filesystem. Note that paths in the
.dockerignorefile are not copied. This means that
RUN npm install: Install
npmcomponents listed in
COPY --from=build ...: Copy existing files in the new
buildimage in the
/etc/os-releasemust be copied to provide the distribution information required by node.
/usrc/src/app/index.jsis the original
ExpressJSsource code file.
--help option for detailed information on using KraftCloud:
Or visit the CLI Reference.