Build a rock for an Express app¶
In this tutorial, we’ll containerise a simple Express app into a rock
using Rockcraft’s expressjs-framework
extension.
It should take 25 minutes for you to complete.
You won’t need to come prepared with intricate knowledge of software packaging, but familiarity with Linux paradigms, terminal operations, and Express is required.
Once you complete this tutorial, you’ll have a working rock for an Express
app. You’ll gain familiarity with Rockcraft and the
expressjs-framework
extension, and have the experience to create
rocks for Express apps.
Setup¶
We recommend starting from a clean Ubuntu installation. If we don’t have one available, we can create one using Multipass:
Is Multipass already installed and active? Check by running
snap services multipass
If we see the multipass
service but it isn’t “active”, then we’ll
need to run sudo snap start multipass
. On the other hand, if we get
an error saying snap "multipass" not found
, then we must install
Multipass:
sudo snap install multipass
See Multipass installation instructions, switch to Windows in the drop down.
See Multipass installation instructions, switch to macOS in the drop down.
Then we can create the VM with the following command:
multipass launch --disk 10G --name rock-dev 24.04
Finally, once the VM is up, open a shell into it:
multipass shell rock-dev
LXD will be required for building the rock. Make sure it is installed and initialised:
sudo snap install lxd
lxd init --auto
In order to create the rock, we’ll install Rockcraft with classic confinement, which grants it access to the whole file system:
sudo snap install rockcraft --classic --channel latest/edge
This tutorial requires the latest/edge
channel of Rockcraft as the
framework is currently experimental.
We’ll use Docker to run the rock. We can install it as a snap
:
sudo snap install docker
By default, Docker is only accessible with root privileges (sudo
). We want
to be able to use Docker commands as a regular user:
sudo addgroup --system docker
sudo adduser $USER docker
newgrp docker
Restart Docker:
sudo snap disable docker
sudo snap enable docker
Note that we’ll also need a text editor. We can either install one of our
choice or simply use one of the already existing editors in the Ubuntu
environment (like vi
).
This tutorial requires the latest/edge
channel of Rockcraft. Run
sudo snap refresh rockcraft --channel latest/edge
to get the latest
edge version.
In order to test the Express app locally, before packing it into a rock, install NPM and initialize the starter app.
sudo apt-get update -y && sudo apt-get install npm -y
Create the Express app¶
Start by creating the “Hello, world” Express app that we’ll pack in this tutorial.
Create an empty project directory:
mkdir expressjs-hello-world
cd expressjs-hello-world
Next, create a skeleton for the project with the Express app generator:
sudo npm install -g express-generator@4
express app
cd app && npm install
Let’s run the Express app to verify that it works:
npm start
The app starts an HTTP server listening on port 3000
that we can test by using curl to send a request to the root
endpoint. We may need a new terminal for this – run
multipass shell rock-dev
to get another terminal:
curl --fail localhost:3000
The Express app should respond with Welcome to Express web page.
Note
The response from the Express app includes HTML and CSS which
makes it difficult to read on a terminal. Visit http://localhost:3000
using a browser to see the fully rendered page.
The Express app looks good, so let’s stop it for now
with Ctrl + C, then move out of the app directory
cd ..
.
Pack the Express app into a rock¶
Now let’s create a container image for our Express app. We’ll use a rock, which is an OCI-compliant container image based on Ubuntu.
First, we’ll need a rockcraft.yaml
project file. We’ll take advantage of a
pre-defined extension in Rockcraft with the --profile
flag that caters
initial rock files for specific web app frameworks. Using the
Express profile, Rockcraft automates the creation of
rockcraft.yaml
and tailors the file for an Express app.
From the ~/expressjs-hello-world
directory, initialize the rock:
rockcraft init --profile expressjs-framework
The rockcraft.yaml
file will automatically be created and set the name
based on your working directory.
Check out the contents of rockcraft.yaml
:
cat rockcraft.yaml
The top of the file should look similar to the following snippet:
name: expressjs-hello-world
# see https://documentation.ubuntu.com/rockcraft/en/latest/explanation/bases/
# for more information about bases and using 'bare' bases for chiselled rocks
base: bare # as an alternative, a ubuntu base can be used
build-base: [email protected] # build-base is required when the base is bare
version: '0.1' # just for humans. Semantic versioning is recommended
summary: A summary of your ExpressJS app # 79 char long summary
description: |
This is expressjs-hello-world's description. You have a paragraph or two to tell the
most important story about it. Keep it under 100 words though,
we live in tweetspace and your description wants to look good in the
container registries out there.
# the platforms this rock should be built on and run on.
# you can check your architecture with `dpkg --print-architecture`
platforms:
amd64:
# arm64:
# ppc64el:
# s390x:
...
Verfiy that the name
is expressjs-hello-world
.
Ensure that platforms
includes the architecture of your host. Check
the architecture of your system:
dpkg --print-architecture
Edit the platforms
key in rockcraft.yaml
if required.
As the expressjs-framework
extension is still experimental, export the
environment variable ROCKCRAFT_ENABLE_EXPERIMENTAL_EXTENSIONS
:
export ROCKCRAFT_ENABLE_EXPERIMENTAL_EXTENSIONS=true
Pack the rock:
rockcraft pack
Warning
There is a known connectivity issue with LXD and Docker. If we see a
networking issue such as “A network related operation failed in a context
of no network access” or Client.Timeout
, allow egress network traffic
to flow from the LXD managed bridge using:
iptables -I DOCKER-USER -i <network_bridge> -j ACCEPT
ip6tables -I DOCKER-USER -i <network_bridge> -j ACCEPT
iptables -I DOCKER-USER -o <network_bridge> -m conntrack \
--ctstate RELATED,ESTABLISHED -j ACCEPT
ip6tables -I DOCKER-USER -o <network_bridge> -m conntrack \
--ctstate RELATED,ESTABLISHED -j ACCEPT
Run lxc network list
to show the existing LXD managed bridges.
Depending on the network, this step can take a couple of minutes to finish.
Once Rockcraft has finished packing the Express rock, we’ll find a new file in
the working directory (an OCI image) with the .rock
extension:
ls *.rock -l --block-size=MB
Run the Express rock with Docker¶
We already have the rock as an OCI image. Now we need to load it into Docker. Docker requires rocks to be imported into the daemon since they can’t be run directly like an executable.
Copy the rock:
sudo rockcraft.skopeo copy \
--insecure-policy \
oci-archive:expressjs-hello-world_0.1_$(dpkg --print-architecture).rock \
docker-daemon:expressjs-hello-world:0.1
This command contains the following pieces:
--insecure-policy
: adopts a permissive policy that removes the need for a dedicated policy file.oci-archive
: specifies the rock we created for our Express app.docker-daemon
: specifies the name of the image in the Docker registry.
Check that the image was successfully loaded into Docker:
sudo docker images expressjs-hello-world:0.1
The output should list the Express image, along with its tag, ID and size:
REPOSITORY TAG IMAGE ID CREATED SIZE
expressjs-hello-world 0.1 30c7e5aed202 2 weeks ago 304MB
Now we’re finally ready to run the rock and test the containerised Express app:
sudo docker run --rm -d -p 3000:3000 \
--name expressjs-hello-world expressjs-hello-world:0.1
Use the same curl command as before to send a request to the Express app’s root endpoint which is running inside the container:
curl --fail localhost:3000
The Express app again responds with Welcome to Express page.
View the app logs¶
When deploying the Express rock, we can always get the app logs with Pebble:
sudo docker exec expressjs-hello-world pebble logs expressjs
As a result, Pebble will give us the logs for the
expressjs
service running inside the container.
We should expect to see something similar to this:
app@0.0.0 start
node ./bin/www
GET / 200 62.934 ms - 170
We can also choose to follow the logs by using the -f
option with the
pebble logs
command above. To stop following the logs, press Ctrl +
C.
Stop the app¶
Now we have a fully functional rock for a Express app! This concludes the first part of this tutorial, so we’ll stop the container and remove the respective image for now:
sudo docker stop expressjs-hello-world
sudo docker rmi expressjs-hello-world:0.1 --force
Update the Express app¶
For our final task, let’s update our app. As an example,
let’s add a new /time
endpoint that returns the current time.
Start by creating the app/routes/time.js
file in a text editor and paste the
code from the snippet below:
var express = require('express');
var router = express.Router();
router.get('/', function (req, res, next) {
res.send(Date());
});
module.exports = router;
Place the code snippet below in app/app.js
under routes registration section
along with other app.use(...)
lines.
It will register the new /time
endpoint:
var timeRouter = require('./routes/time');
app.use('/time', timeRouter);
Since we are creating a new version of the app, set
version: '0.2'
in the project file.
The top of the rockcraft.yaml
file should look similar to the following:
name: expressjs-hello-world
# see https://documentation.ubuntu.com/rockcraft/en/latest/explanation/bases/
# for more information about bases and using 'bare' bases for chiselled rocks
base: bare # as an alternative, a ubuntu base can be used
build-base: [email protected] # build-base is required when the base is bare
version: '0.2'
summary: A summary of your ExpressJS app # 79 char long summary
description: |
This is expressjs-hello-world's description. You have a paragraph or two to tell the
most important story about it. Keep it under 100 words though,
we live in tweetspace and your description wants to look good in the
container registries out there.
# the platforms this rock should be built on and run on.
# you can check your architecture with `dpkg --print-architecture`
platforms:
amd64:
# arm64:
# ppc64el:
# s390x:
Pack and run the rock using similar commands as before:
rockcraft pack
sudo rockcraft.skopeo --insecure-policy \
copy oci-archive:expressjs-hello-world_0.2_$(dpkg --print-architecture).rock \
docker-daemon:expressjs-hello-world:0.2
sudo docker images expressjs-hello-world:0.2
sudo docker run --rm -d -p 3000:3000 \
--name expressjs-hello-world expressjs-hello-world:0.2
The resulting .rock
file will be named differently, as
its new version will be part of the filename.
Finally, use curl to send a request to the /time
endpoint:
curl --fail localhost:3000/time
The updated app should respond with the current date and time (e.g.
Fri Jan 10 2025 03:11:44 GMT+0000 (Coordinated Universal Time)
).
Tip
If you are getting a 404
for the /time
endpoint, check the
Troubleshooting steps below.
Final Cleanup¶
We can now stop the container and remove the corresponding image:
sudo docker stop expressjs-hello-world
sudo docker rmi expressjs-hello-world:0.2 --force
Reset the environment¶
We’ve reached the end of this tutorial.
If we’d like to reset the working environment, we can simply run the following:
# delete all the files created during the tutorial
sudo npm uninstall -g express-generator@4
sudo apt-get remove npm -y
rm -rf app
rm expressjs-hello-world_0.1_$(dpkg --print-architecture).rock \
expressjs-hello-world_0.2_$(dpkg --print-architecture).rock \
rockcraft.yaml
We can also clean the Multipass instance up. Start by exiting it:
exit
And then we can proceed with its deletion:
multipass delete rock-dev
multipass purge
Next steps¶
Troubleshooting¶
App updates not taking effect?
Upon changing the Express app and re-packing the rock, if
the changes are not taking effect, try running rockcraft clean
and pack
the rock again with rockcraft pack
.