Install slices in a rock¶
In this tutorial, you will create a lean hello-world rock that uses chisel slices, and then compare the resulting rock with the one created without slices in Create a “Hello World” rock.
Setup your environment¶
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 need to install Rockcraft:
sudo snap install rockcraft --classic
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
sudo snap disable docker
sudo snap enable docker
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”, make sure to apply one of the suggested fixes here.
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
).
Project setup¶
Create a new directory, write the following into a text editor and save it as
rockcraft.yaml
:
name: chiselled-hello
summary: Hello world from Chisel slices
description: A "bare" rock containing the "hello" package binaries from Chisel slices.
license: Apache-2.0
version: "latest"
base: bare
build-base: "[email protected]"
platforms:
amd64:
parts:
hello:
plugin: nil
stage-packages:
- hello_bins
Note that this Rockcraft file uses the hello_bins
Chisel slice to generate
an image containing only files that are strictly necessary for the hello
binary. See Chisel for details on the Chisel tool.
Pack the rock with Rockcraft¶
To build the rock, run:
rockcraft pack
The output will look similar to:
Launching instance...
Retrieved base bare for amd64
Extracted bare:latest
Executed: pull hello
Executed: overlay hello
Executed: build hello
Executed: stage hello
Executed: prime hello
Executed parts lifecycle
Exported to OCI archive 'chiselled-hello_latest_amd64.rock'
The process might take a little while, but at the end, a new file named
chiselled-hello_latest_amd64.rock
will be present in the current directory.
That’s your chiselled-hello rock, in oci-archive format.
Run the rock in Docker¶
First, import the recently created rock into Docker:
sudo rockcraft.skopeo --insecure-policy copy oci-archive:chiselled-hello_latest_amd64.rock docker-daemon:chiselled-hello:latest
Now you can run a container from the rock:
docker run --rm chiselled-hello:latest exec hello -t
Which should print:
hello, world
The chiselled-hello
image will have a size of 5.6 MB, which is much less in
size than the 8.8 MB hello
rock created in Create a “Hello World” rock.