Pack a Python package¶
By the end of this tutorial you will be able to run pyfiglet via docker:
$ docker run --rm -it pyfiglet:1.0.4 exec pyfiglet hello _ _ _ | |__ ___| | | ___ | '_ \ / _ \ | |/ _ \ | | | | __/ | | (_) | |_| |_|\___|_|_|\___/
It should take around 10 minutes to complete.
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
snapservicesmultipass
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:
sudosnapinstallmultipass
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 --memory 2G --disk 10G --name rock-dev 26.04
Finally, once the VM is up, open a shell into it:
multipassshellrock-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:
sudosnapinstalllxd lxdinit--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"|sudotee-a/etc/sysctl.d/99-forwarding.conf sudosystemctlrestartsystemd-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:
sudosnapinstallrockcraft--classic
We’ll use Docker to run the rock. We can install it as a snap:
sudosnapinstalldocker
By default, Docker is only accessible with root privileges (sudo). We want
to be able to use Docker commands as a regular user:
sudoaddgroup--systemdocker
sudoadduser$USERdocker
newgrpdocker
Restart Docker:
sudosnapdisabledocker
sudosnapenabledocker
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).
Project setup¶
To create a new Rockcraft project, create a new directory and change into it:
mkdirpyfiglet-rock&&cdpyfiglet-rock
Next, create a file called rockcraft.yaml with the following contents:
name:pyfiglet base:ubuntu@26.04 version:"1.0.4"# Note: should match `pyfiglet` below summary:A rock for pyfiglet description:A rock for pyfiglet license:Apache-2.0 platforms: amd64: parts: pyfiglet: plugin:python source:. python-packages: -pyfiglet==1.0.4# Note: should match `version` above stage-packages: -python3-venv
Note
This example uses python plugin along python-packages, so pip is
invoked implicitly to install such packages. PEP 668 changes the
behavior of Python package managers like pip by introducing a special
marker (EXTERNALLY-MANAGED) that prevents pip from installing
packages outside a virtual environment. Thus, python3-venv is required
as a stage package.
Pack the rock with Rockcraft¶
To build the rock, run:
rockcraftpack
Run the rock in Docker¶
First, import the recently created rock into Docker:
sudorockcraft.skopeo--insecure-policycopyoci-archive:pyfiglet_1.0.4_amd64.rockdocker-daemon:pyfiglet:1.0.4
Now run the pyfiglet command from the rock:
dockerrun--rmpyfiglet:1.0.4execpyfigletitworks!
Which should print:
_ _ _ _ (_) |_ __ _____ _ __| | _____| | | | __| \ \ /\ / / _ \| '__| |/ / __| | | | |_ \ V V / (_) | | | <\__ \_| |_|\__| \_/\_/ \___/|_| |_|\_\___(_)
Explore the running container¶
Since the rock uses an ubuntu base, you can poke around in a running container using bash, via:
$ docker run --rm -it pyfiglet:1.0.4 exec bash root@14d1812a2681:/# pyfiglet hi _ _ | |__ (_) | '_ \| | | | | | | |_| |_|_|