Skip to content

podman

podman

Podman helpers shared by the terok packages.

Exposes helpers that spell podman arguments correctly for the host's podman release: user-namespace mapping for rootless invocations, the force-pull flag for image builds, and the base of a container's /etc/hosts.

__all__ = ['podman_hosts_file_args', 'podman_pull_always_args', 'podman_userns_args'] module-attribute

podman_userns_args()

Return user namespace args for rootless podman so UID 1000 maps correctly.

Maps the host user to container UID/GID 1000, the conventional non-root dev user in terok container images. Returns an empty list when running as root — root podman runs in the host user namespace and has no need to remap.

Podman releases older than 4.3 predate the keep-id:uid=… customization; for those the same mapping is spelled out as explicit --uidmap/--gidmap triples, assuming the standard 65536-ID subordinate allotment.

Source code in src/terok_util/podman.py
def podman_userns_args() -> list[str]:
    """Return user namespace args for rootless podman so UID 1000 maps correctly.

    Maps the host user to container UID/GID 1000, the conventional non-root
    ``dev`` user in terok container images.  Returns an empty list when
    running as root — root podman runs in the host user namespace and
    has no need to remap.

    Podman releases older than 4.3 predate the ``keep-id:uid=…``
    customization; for those the same mapping is spelled out as explicit
    ``--uidmap``/``--gidmap`` triples, assuming the standard 65536-ID
    subordinate allotment.
    """
    if os.geteuid() == 0:
        return []
    if _podman_version() >= _KEEP_ID_UID_MIN:
        return [f"--userns=keep-id:uid={_DEV_ID},gid={_DEV_ID}"]
    mappings = (
        f"0:1:{_DEV_ID}",  # container 0..dev-1  <- subordinate IDs
        f"{_DEV_ID}:0:1",  # container dev       <- the host user itself
        f"{_DEV_ID + 1}:{_DEV_ID + 1}:{_ID_SPAN - _DEV_ID - 1}",  # the rest, identity
    )
    return [arg for flag in ("--uidmap", "--gidmap") for m in mappings for arg in (flag, m)]

podman_pull_always_args()

Return the podman build flags that force re-pulling base images.

Podman 4.0 turned --pull into a policy option (--pull=always); older releases take --pull as a boolean and reject the policy form outright, spelling the same request --pull-always instead.

Source code in src/terok_util/podman.py
def podman_pull_always_args() -> list[str]:
    """Return the ``podman build`` flags that force re-pulling base images.

    Podman 4.0 turned ``--pull`` into a policy option (``--pull=always``);
    older releases take ``--pull`` as a boolean and reject the policy
    form outright, spelling the same request ``--pull-always`` instead.
    """
    if _podman_version() >= _PULL_POLICY_MIN:
        return ["--pull=always"]
    return ["--pull-always"]

podman_hosts_file_args(base)

Return the podman run flags that set the base of the container's /etc/hosts.

Podman builds that file from a base file. Its default base is the host's /etc/hosts, which copies private host names into the container. Podman older than 5.4 has no way to change the base per container: this returns no flags there, and tells the operator.

Parameters:

Name Type Description Default
base str

A --hosts-file value: none, image, or an absolute host path.

required
Source code in src/terok_util/podman.py
def podman_hosts_file_args(base: str) -> list[str]:
    """Return the ``podman run`` flags that set the base of the container's ``/etc/hosts``.

    Podman builds that file from a base file.  Its default base is the
    host's ``/etc/hosts``, which copies private host names into the
    container.  Podman older than 5.4 has no way to change the base per
    container: this returns no flags there, and tells the operator.

    Args:
        base: A ``--hosts-file`` value: ``none``, ``image``, or an absolute host path.
    """
    if _podman_version() < _HOSTS_FILE_MIN:
        print("Podman is older than 5.4: containers copy the host's /etc/hosts.", file=sys.stderr)
        return []
    return ["--hosts-file", base]