Containerise a PyPI package

By the end of this tutorial you will be able to run pyfiglet via docker:

$ docker run --rm -it pyfiglet:0.7.6 exec pyfiglet hello
 _          _ _
| |__   ___| | | ___
| '_ \ / _ \ | |/ _ \
| | | |  __/ | | (_) |
|_| |_|\___|_|_|\___/

Setup your environment

We recommend starting from a clean Ubuntu 22.04 installation. If you don’t have one available, you can create one using Multipass:

Is Multipass already installed and active? Check by running

snap services multipass

If you see the multipass service but it isn’t “active”, then you’ll need to run sudo snap start multipass. On the other hand, if you get an error saying snap "multipass" not found, then you must install Multipass:

sudo snap install multipass

Then you can create the VM with the following command:

multipass launch --disk 10G --name rock-dev 22.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, you’ll need to install Rockcraft:

sudo snap install rockcraft --classic

We’ll use Docker to run the rock. You can install it as a snap:

sudo snap install docker

Warning

There is a known connectivity issue with LXD and Docker. If you see a networking issue such as “A network related operation failed in a context of no network access”, make sure you apply one of the fixes suggested here.

Note that you’ll also need a text editor. You can either install one of your choice or simply use one of the already existing editors in your Ubuntu environment (like vi).

Project setup

To create a new Rockcraft project, create a new directory and change into it:

mkdir pyfiglet-rock && cd pyfiglet-rock

Next, create a file called rockcraft.yaml with the following contents:

name: pyfiglet
base: [email protected]
version: '0.7.6'  # 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==0.7.6  # Note: should match `version` above
    stage-packages:
      - python3-venv

Pack the rock with Rockcraft

To build the rock, run:

rockcraft pack

Run the rock in Docker

First, import the recently created rock into Docker:

sudo rockcraft.skopeo --insecure-policy copy oci-archive:pyfiglet_0.7.6_amd64.rock docker-daemon:pyfiglet:0.7.6

Now run the pyfiglet command from the rock:

docker run --rm pyfiglet:0.7.6 exec pyfiglet it works!

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:0.7.6 exec bash
root@14d1812a2681:/# pyfiglet hi
 _     _
| |__ (_)
| '_ \| |
| | | | |
|_| |_|_|