Build a rock for a Django app

In this tutorial, we’ll create a simple Django app and learn how to containerise it in a rock, using Rockcraft’s django-framework extension.

It should take 25 minutes for you to complete.

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

Then we can create the VM with the following command:

multipass launch --memory 2G --disk 10G --name rock-dev 26.04

Finally, once the VM is up, open a shell into it:

multipass shell rock-dev

Unless stated otherwise, we will work entirely within the VM from now on.


LXD will be required for building the rock. Make sure it is installed and initialised:

sudo snap install lxd
lxd init --auto

There is a known connectivity issue with LXD and Docker. To avoid this issue, enable IPv4 forwarding before installing Docker:

echo "net.ipv4.conf.all.forwarding=1" | sudo tee -a /etc/sysctl.d/99-forwarding.conf
sudo systemctl restart systemd-sysctl

Warning

In a production environment, make sure to use a hardened firewall configuration.

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

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 nano).

Finally, create an empty project directory:

mkdir django-tutorial
cd django-tutorial

We’ll name the outer workspace directory ~/django-tutorial/ so we can distinguish it from the Django project (django_hello_world) and the rock artifact (django-hello-world). Rockcraft will generate a project file named