Podtroduction to Podman Quadlets for the Compose Minded

—

Dear docker-composeriser,

What did you just call me? #

Self-hosting is all the rage these days, and what better way to self-host software than using docker-compose podman quadlets?

Podman is RedHat’s answer to containerisation, and quadlet is to podman what compose is to docker. Quadlet natively integrates with systemd (they’re ‘merely’ generate systemd services that run podman cli commands) and support some neat security features out of the box.

As they are ‘merely’ generated systemd services, this means their integration with systemd is off the charts, any monitoring tooling for units will work for quadlet! No more silly parallel logging systems (stupid docker).

Furthermore, rootless comes by default due to podmans daemonless nature (as opposed to the docker daemon which runs all the time).

One might then think, if quadleting is so great, then why the heck is everyone composing? There are a few reasons:

The quad for caseman podlets #

First I want to talk about the benefits that podman quadlet provides. For starters quadlet integrates with systemd so you get journald logging and systemd service templating as freebies, along with all the systemd service magic regarding % placeholders.

The gifts don’t just stop there, if you are running a rootful setup (which I recommend for self-hosting) then you can just pop a UserNS=auto into your quadlet definition to have the entire container run as an unprivileged user automatically (granted you need to allocate a block of subuids and subgids first for podman to pick from).

A little side note: many containers will switch to a rootless user despite their root user being mapped to an unprivileged user already, I believe this is an artifact from docker being a bit annoying to run rootless, thus many people just rawdogged self-hosting as root, as a consequence some containers (or the containerised software) will deprivilege itself to protect the user. The user switching can be a little annoying when combined with NoNewPrivileges=true as the setting stops you from changing setuid bits(?) which can cause some deprivileging scripts to fail. NoNewPrivileges=true prevents privilege escalation so not being able to use it can be annoying.

Okay but what the heck is a quadlet? #

Good question! Podman quadlets are any files ending in .container, .pod, .image, .build, .network, .volume or .kube. If you want podman to pick up on these files you need to place them in the /etc/containers/systemd directory. From there you have 2 options when it comes to placing the quadlet files which determine how the containers are run

You may run a rootful setup by placing it in /etc/containers/systemd., and a rootless setup by placing it /etc/containers/systemd/user/${UID} (or ~/.config/containers/systemd). Unsurprisingly any quadlet file placed in etc/containers/systemd/user/${UID} will run under the user ${UID}. For this rest of this post we assume a rootful setup is at play, since some options only work for rootful setups, furthermore containers can only be at most as privileged as the user that spawned them (which is important for binding ports under 1024 and device passthrough). For some rootless setups you need to allow the user to linger and will need to add the --user flag to systemd commands.

You may place more subdirecties in the /etc/containers/systemd directory for organisational purposes, podman will pick up on all of them.

There are a quite a few file types when it comes to quadlet, but we will focus on the following two: .container and .pod, as these are the most important.

Drop the following file into /etc/containers/systemd/hello.container

[Unit]
Description=Hello Alpine Container

[Container]
Image=docker.io/alpine
ContainerName=hello
Exec=echo Hello from Quadlet!

[Install]
WantedBy=default.target

Now run systemctl daemon-reload, then systemctl restart hello. systemctl daemon-reload turns the hello.container file into a systemd service, you don’t have to enable this service (and in fact, you cannot, as it is a generated service), but you do have to start it, hence the systemctl restart.

You should notice in journalctl -u hello that a nice message was posted for you. Congratulations, your first quadlet container!

Cool, but what about the pod in podman, man? (Title Drop) #

Dropping a bunch of .container files clearly isn’t a great idea, what if you need 7 different instances of postgres that all bind to the same port? What if you want to isolate unrelated containers from one another? What if you need certain containers to be able to talk to each other on their own network using dns names? What if you want a bunch of containers to act as one logical …pod?

That’s where .pod comes in. Essentially a pod binds a bunch of containers into a shared network namespace and linux user namespace (optional). All the container in the pod can be stopped and restarted as one logical unit too!

To create a pod you simply have to define /etc/containers/systemd/my-fabulous-pod.pod

[Pod]
PodName=my-fabulous-pod

and to our previous container in /etc/containers/systemd/hello.container we add the following line

@@ -2,6 +2,7 @@
 Description=Hello Alpine Container
 
 [Container]
+Pod=my-fabulous-pod.pod
 ContainerName=hello
 Image=docker.io/alpine

Run systemctl daemon-reload and then systemctl restart my-fabulous-pod-pod. Inspect the logs again with journalctl -u hello. You might notice the command to inspect the container logs didn’t change, that is because podman pods do not namespace container names, so container names have to be globally unique, which kind of sucks, the same applies to volume names.

We can add as many .containers to our pod as we like just by adding the line Pod=my-fabulous-pod.pod under [Container]. All containers in the pod will share the same network namespace meaning that they are inaccessible from the outside work unless you publish the ports in the .pod file with

[Pod]
PodName=my-fabulous-pod
PublishPort=80:8080

as a bonus you can run podman ps to see all containers and their current state. Keep in mind that all quadlet files get translated to systemd services so you can always inspect their logs with journalctl -[xe]u $container-name for debugging purposes

That’s cool buddy, but where are these safety features you promised? #

Podman (and quadlet subsequently) has a neat security feature called UserNS=auto which is more secure than whatever docker does. Docker does provider a similar feature called userns-remap, but you will notice that it does not have an auto mode. Suppose we were only using containers (no pod), adding UserNS=auto under the [Container] section of our hello.container

@@ -2,10 +2,9 @@
 Description=Hello Alpine Container
 
 [Container]
-Pod=my-fabulous-pod.pod
 Image=docker.io/alpine
 ContainerName=hello
 Exec=echo Hello from Quadlet!
+UserNS=auto
 
 [Install]
 WantedBy=default.target

then this hello container will be placed in its own (unprivileged) linux usernamespace! This means that the root id in the container is mapped to a unprivileged user on the host system.

If you want to use user namespaces with podman, you must ensure that for rootful setups the containers user has a sufficiently large range of sub(g|u)ids to use, inspect your /etc/subuid and /etc/subgid files to check if this is the case, otherwise you may run the following

echo "containers:2147483647:2147483648" | sudo tee -a /etc/subuid
echo "containers:2147483647:2147483648" | sudo tee -a /etc/subgid
podman system migrate

Of course you may pick a smaller block of (g|u)ids. The reason we don’t use usermod is that usermod gets really upset when you use big numbers. podman system migrate makes sure podman is aware that it can pick from this block of sub(g|u)ids.

For rootless setups you want to the user that the container will be running under to have a block of sub(g|u)ids to allocate from.

You can also use the SubGIDMap and SubUIDMap options to isolate pods/containers instead of UserNS, this is achieved by giving<some-user-name> a sub(g|u)id block and assigning Sub(G|U)IDMap = <some-user-name>.

Now podman will pick a random unprivileged uid and subgid from the block we setup and run the container with that! The container will think it is root but it actually is not, to make this even more secure we can add NoNewPrivileges=true, DropCapablity=All, and ReadOnly=true under [Container].

There is a small issue though, this only runs our hello.container as an unprivileged user, but how does this interact with pods? Well my dear reader, naively you would assume that you would have to stick a UserNS=auto in every single .container even if they belong to the same .pod.

You do not! You should not! You shall not!

In fact you only need a UserNS=auto (this is a different one than before) in your .pod under [Pod], and it will put all the containers belonging to the pod in the same user namespace. There is no point in then adding UserNS=auto under each [Container], it will just become a weird mess of nested namespaces and sub(g|u)id allocations.

You should verify that all pods are being assigned different sub(g|u)ids, as I’ve had that different pods overlapped, but that might have been user configuration error. Personally I ended up using SubGIDMap and SubUIDMap under [Pod] and giving each pod a user in /etc/sub(g|u)id, as this will sorely vex podman if the mappings do not exist instead of ‘doing the wrong thing silently’. usermod will nicely manage these ids in blocks of 2162^{16}, which is a good size as some containers like to be annoying and use the linux nobody user which is id 65534, forcing you to do UserNS=auto:size=65535.

Wait, what? NoNewPrivileges, ReadOnly, DropCapabilities, SELinux? #

Yes you heard me right (although I didn’t mention SELinux so how did you know?)! These are nice security features (per container, not pod) that securely secure your container from tampering.

Podman (quadlet) also integrates with SELinux. If your distribution supports SELinux then you can do neat things like add :Z or :z labels to volume mounts, this ensures your container are the only one able to access that particular part of the fs.

As I write to you in good faith, I will mention that docker also has all of these capabilities bar the SELinux integration, although I have yet to see popular self-hosting projects apply these options by default.

I still want to docker my compose! #

If you don’t care about the security and systemd integration maybe you will like the .build, .volume, or.image quadlets.

I’m not buying what you’re selling buddy #

Fine, it’s not the absolute best idea but podman quadlet can self-update, is that what you wanted to hear?! Tsk.

In each .container file, if you add AutoUpdate=registry under [Container] then the image used by the container will automatically update. It does this by polling the image and seeing if it has changed. One might wrongly think that merely adding the aformentioned line suffices in unlocking our automatic updater, alas one must run systemctl enable podman-auto-update.timer to enable this coveted functionality. As this is just a regular systemd time, one can change the periodicity of this timer. Note that for (some) rootless setups this must be run per user.

Podman …Quaderletes? #

I can’t really be arsed to write about this since I haven’t used it, but you can write a limited kubernetes manifest and then write a .kube file that will run the manifest, the advantages it has over regular quadlets is that container names are namespaced to the pod (although named volumes are still global) and that you can define everything in one yaml file. Other than that, meh. I believe it is more for testing kubernetes manifests locally with ease rather than a quadlet alternative.

Some caveats #

At least it doesn’t do that stupid docker compose thing where if you aren’t in the right directory with the compose file you can’t run commands on containers that are running currently.

Final words #

Podman quadlet is a good alternative to docker compose. Especially since the docker company made their logo ugly while podmans is three cute selkies. Podman can seriously benefit a power user; look at all the options in the documentation for customisation. Higher level resources are freely avaiable for the podman (quadlet) ecosystem, it is only the introductory resources that are lacking.

If you are hesistant about switching you can test podman quadlet out with the podlet tool to convert your existing compose setup to quadlet. I know ‘conclusions’ are not supposed to introduce ’new’ information to the ‘audience’, but I think another docker compose alternative worth mentioning is incuse compose. Personally I still run podman quadlet on IncusOS though instead of incus-compose.

I hope I was able to convinence you to give podman (quadlet) a try!

Pod podgards,

Podgus Young

Further reading #