Skip to content

Run containers from systemd

Running containers from systemd

When you embed a container in an operating system (OS) image, you can start the container manually in the booted system with the podman run command. However, the container does not start automatically at boot time. To configure a container to start at boot time, you must create a systemd service that starts the container at the right time, in the right way.

Quadlet is a tool that optimally runs Podman containers under systemd. Rather than creating the systemd service manually, use Quadlet to automatically generate the corresponding systemd service unit file at boot time. Quadlet simplifies container management by allowing you to create declarative configurations in .container files instead of lengthy, complex systemd unit files.

Quadlet unit files configure containers in both the root and QM partitions. In the AutoSD OS filesystem, store your .container files in /etc/containers/systemd.

At a minimum, include the following sections and options in a .container file:

[Unit]
Description=<A human-readable title>

[Container]
Image=<The container image>
Exec=<The command that runs in the container>

[Install]
WantedBy=multi-user.target default.target

Include additional sections and options to meet your container requirements. Any section or option permitted in a systemd.unit file is also permitted in a Quadlet unit file.

Containers within containers

The QM partition is a container within the root partition of an AutoSD OS image. The QM partition houses containers that run non-critical application workloads.

To add containers to the QM partition, you create Quadlet unit files and add them to the add_files section of your Automotive Image Builder manifest, for example:

```YAML title="Quadlet unit file added to an Automotive Image Builder manifest" --8<-- "demos/ipc_between_qm_root_2/ipc_between_qm_root_2.aib.yml:39:40" ... --8<-- "demos/ipc_between_qm_root_2/ipc_between_qm_root_2.aib.yml:59:61"


The example shows an `ipc_client.container` unit file added to the `add_files` section of the `qm` partition. The `path` section is the location
for all container files, `/etc/containers/systemd`.

You can also create Quadlet unit files directly in the manifest, for example:

```YAML title="Quadlet unit file created in the Automotive Image Builder manifest"
    --8<-- "demos/container_qm_network/container_qm_network.aib.yml:27:27"
    ...
    --8<-- "demos/container_qm_network/container_qm_network.aib.yml:31:31"
    ...
    --8<-- "demos/container_qm_network/container_qm_network.aib.yml:37:45"

The example shows an nginx container image created directly from the manifest. The text section contains the contents of the .container file.

Configuring communication between QM containers demonstrates additional Quadlet unit file use cases.

Creating Quadlet files for sample applications

In this example, create Quadlet files for the sample applications that are available in the AutoSD sample-apps repository. If you want to use your own containerized software, see the Podman documentation for more information about creating your own Quadlet configuration files.

Prerequisites

Procedure

  1. Create a systemd socket unit and Quadlet unit files for the radio-service and engine-service services in your sample application auto-apps:

    console title="engine.container file" --8<-- "demos/ipc_root_root/engine.container"

    console title="radio.socket file" --8<-- "demos/ipc_root_root/radio.socket"

    console title="radio.container file" --8<-- "demos/ipc_root_root/radio.container"

Note

Requires=radio.socket is a hard dependency that prevents the radio service from starting until the socket unit is active. After=radio.socket is an ordering dependency: the radio service starts only after the socket unit has started. Requires=routingmanagerd.service is a hard dependency that prevents the radio service from starting until the routing manager service is active. Wants=engine.service is a soft dependency. Ideally, the engine service activates the radio service, but if the engine service is not activated, systemd still permits the radio service to start.

  1. Create an Automotive Image Builder manifest named root-root.aib.yml that contains the following code, which copies the Quadlet unit files to the /etc/containers/systemd/ directory during the OS image build process:

    yaml title="Manifest configuration to copy Quadlet unit files" --8<-- "demos/ipc_root_root/root-root.aib.yml"

    Note

    The source_path: option resolves a relative path. In this example, the Quadlet unit files are in the same directory as the manifest.

  2. Run aib-dev build to build a disk image:

    console $ aib-dev \ --include=/var/lib/containers/storage/ \ build \ --distro autosd10 \ --target qemu \ --build-dir=_build \ root-root.aib.yml \ root-root.$arch.qcow2

  3. Verify that the script has created an AutoSD image file named root-root.<arch>.qcow2 in your present working directory.

  4. Assuming that you have installed QEMU, boot the AutoSD image in a virtual machine, substituting the name of your .qcow2 image file (if necessary):

    console $ air --nographics root-root.x86_64.qcow2

  5. After the image boots in QEMU, log in with the user name root and the password password.

Additional resources