[meta-virtualization][PATCH 2/7] recipes-containers/images: add app-container-python
Tim Orling <[email protected]> Fri, 29 May 2026 18:31:03 -0700
| Newsgroups | org.yoctoproject.lists.meta-virtualization |
|---|---|
| Message-ID | <2c612245431bf2ebcfe9c4f188be1e27117cc0db.1780104071.git.tim.orling@konsulko.com> |
Add OCI container image recipe for Python to use as a base for other Python app containers. The image uses multi-layer mode with separate base, terminal and python layers. Add ncurses-terminfo-base to a "terminal" layer to avoid warnings in the REPL: "Cannot read termcap database; using dumb terminal settings." Add coreutils to "python" layer to provide /usr/bin/env needed by python3-idle in python3-modules. Inherit container-nonroot-user and run a `nonroot` user by default. Set PACKAGECONFIG:pn-app-container-python = "dev" in local.conf or distro/image config to run as 'root' and include 'pip'. Signed-off-by: Tim Orling <[email protected]> --- .../images/app-container-python.bb | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 recipes-containers/images/app-container-python.bb diff --git a/recipes-containers/images/app-container-python.bb b/recipes-containers/images/app-container-python.bb new file mode 100644 index 00000000..a93f1b0f --- /dev/null +++ b/recipes-containers/images/app-container-python.bb @@ -0,0 +1,57 @@ +SUMMARY = "Base python3 container image" +DESCRIPTION = "OCI container image running Python with non-root user. \ +\ +In "dev" mode, can optionally run as 'root' and add 'pip' to allow \ +developers to simply run 'pip install' on top of this container (Not \ +advised for production/hardened use)." +LICENSE = "MIT" +LIC_FILES_CHKSUM = "file://${COREBASE}/meta/COPYING.MIT;md5=3da9cfbcb788c80a0384361b4de20420" + +# Multi-layer mode: create explicit layers instead of single rootfs layer +OCI_LAYER_MODE = "multi" + +# Optional 'dev' mode: +# - adds python3-pip to the python layer (enables `pip install` at runtime) +# - runs the container as root (UID 0) so pip can write to site-packages +# Enable with: PACKAGECONFIG:pn-app-container-python = "dev" +PACKAGECONFIG ??= "" +PACKAGECONFIG[dev] = "" + +# Define layers: each layer contains specific packages +# Format: "name:type:content" where content uses + as delimiter for multiple items +OCI_LAYERS = "\ + base:packages:base-files+base-passwd+netbase \ + terminal:packages:ncurses-terminfo-base \ + python:packages:python3+coreutils${@bb.utils.contains('PACKAGECONFIG', 'dev', '+python3-pip', '', d)} \ +" + +# In 'dev' mode, override the nonroot UID inherited from container-nonroot-user +# so the container runs as root (required for `pip install`). +OCI_IMAGE_RUNTIME_UID = "${@bb.utils.contains('PACKAGECONFIG', 'dev', '0', '${NONROOT_UID}', d)}" + +# Use CMD so `docker run image /bin/sh` works as expected +OCI_IMAGE_CMD = "python3" + +IMAGE_FSTYPES = "container oci" +inherit image +inherit image-oci +inherit container-nonroot-user + +IMAGE_FEATURES = "" +IMAGE_LINGUAS = "" +NO_RECOMMENDATIONS = "1" + +# IMAGE_INSTALL triggers package builds via do_rootfs recrdeptask. +# Even for multi-layer mode, list packages here to ensure they're built. +# The PM will install them directly to layers from DEPLOY_DIR_IPK. +# Note: IMAGE_ROOTFS is still created but ignored for packages layers. +IMAGE_INSTALL = "base-files base-passwd netbase" +IMAGE_INSTALL += "ncurses-terminfo-base" +IMAGE_INSTALL += "python3 coreutils" +IMAGE_INSTALL += "${@bb.utils.contains('PACKAGECONFIG', 'dev', 'python3-pip', '', d)}" + +# Allow build with or without a specific kernel +IMAGE_CONTAINER_NO_DUMMY = "1" + +# Note: No ROOTFS_POSTPROCESS_COMMAND needed - IMAGE_ROOTFS is empty +# and PM handles installation directly to OCI layers -- 2.54.0