Podman

Contrary to the Containers section, this Podman manual is targeted at small instances. It uses SQLite as a database and locally cloned source repository.

Note

This tutorial supports multiple bot instances running under the same system account. When following it, replace $BOTNAME with the name of your bot (or use generic strawberry name, just be consistent). Make sure it does not contain spaces, slashes or other funny characters.

Fetching source code

Use git to download the source code.

Warning

It is necessary to use HTTPS, because the container should not have access to your personal SSH keys. If you need to clone a private repository, you can use GitHub’s Personal Access Tokens with REPO:READ permissions.

git clone https://github.com/strawberry-py/strawberry-py.git $BOTNAME
cd $BOTNAME

To update the bot later, run

cd $BOTNAME
git pull

Discord bot token

See Bot token in chapter General Bot Information.

Creating Strawberry image

podman build --file Dockerfile --tag strawberry-py
# and do the following for all the bots you host like this
podman tag strawberry-py:latest $BOTNAME

The .env file

The environment file contains variables necessary for the bot to function.

# A string passed to SQLAlchemy
DB_STRING=sqlite:////strawberry-py/strawberry.db
# A string used to authenticate to the Discord API
TOKEN=0123456789-abcdefghijk-lmopqrstuv-wxyz
# Space separated list of apt packages to be installed before the bot starts
BOT_EXTRA_PACKAGES=
# Timezone of the server
BOT_TIMEZONE=Europe/Prague

Downloading extension modules

strawberry.py is modular, which means that the core only provides basic functionality. To get more, browse either the official sources or even your own repository with more.

cd modules/
git clone https://github.com/strawberry-py/strawberry-fun fun
cd ..

Start the bot once

This step is used to verify our local setup works.

podman run --name=$BOTNAME \
  --env-file $HOME/$BOTNAME/.env -v $HOME/$BOTNAME:/strawberry-py:z \
  $BOTNAME:latest
# To destroy the container (if you either want to clean up or want to run the command again):
podman conatainer rm $BOTNAME

Start the bot automatically with systemd

To let the bot start and recover automatically, we have to generate a systemd unit file.

As you may have noticed, the previous command is still in the foreground, and blocking the shell. You may either kill it via Ctrl+C command, or run strawberry shutdown via Discord.

Create a .container file. For example, $HOME/.config/containers/systemd/$BOTNAME.container:

[Unit]
Description=$BOTNAME, a strawberry.py Discord bot
After=local-fs.target

[Container]
Image=localhost/$BOTNAME:latest
EnvironmentFile=/home/discord/$BOTNAME/.env
# ...and possibly more options, see https://docs.podman.io/en/latest/markdown/podman-systemd.unit.5.html#container-units-container

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

You can verify the validity of the file by running

/usr/libexec/podman/quadlet -dryrun -user

All that’s left to do now is to restart the local Podman daemon and start the container image with the bot.

systemctl --user daemon-reload
systemctl --user status $BOTNAME.service
systemctl --user start $BOTNAME.service
# and once you know the bot is running and everything worked
systemctl --user enable $BOTNAME.service

Note

Podman 4.4 (Fedora 38, RHEL-like 9.2 systems) seems to be setting the log driver to passthrough, which means that it is not possible to see the logs of the systemd-$BOTNAME container. The LogDriver=journald is not yet available in 4.4, which may result in harder debugging.