Experience
Projects
My Stack
Case studies
Contact me
Open my CV

·5 min read

Forgejo on a Pterodactyl panel: write app.ini once, then only sed what the panel owns

A Pterodactyl server restarts all the time: a panel setting changes, the node reboots, someone presses the button. For the Forgejo image in my yolks repo that created two opposite requirements. The port and public address from the panel have to be current on every start, and everything the owner edited by hand in app.ini has to survive. Regenerating the file each time satisfies the first and destroys the second. Never touching it after the first start does the reverse.

Project
Yolks (Pterodactyl images)
Date
Oct 2026
Stack
Bash, Docker, Forgejo, Pterodactyl
Commit
3dcef72

The two halves of the script

The script, forgejo/forgejo-start, runs inside the container as the unprivileged container user, with everything under /home/container, the folder Pterodactyl mounts as the server's disk. Its header says what it does: on the first start it writes custom/conf/app.ini, migrates the database and creates the administrator; on every start it rewrites the port, the public address and the registration switch from the panel's variables, and "nothing else is touched".

The first half is guarded by a single test:

bash
if [[ ! -f "$CONF" ]]; then
    secret_key=$(forgejo generate secret SECRET_KEY)
    internal_token=$(forgejo generate secret INTERNAL_TOKEN)
    jwt_secret=$(forgejo generate secret JWT_SECRET)
    lfs_jwt_secret=$(forgejo generate secret LFS_JWT_SECRET)
    umask 077
    cat > "$CONF" <<INI
    ...
INI
    umask 022
fi

Secrets are generated by Forgejo's own generate secret command rather than by me, and umask 077 around the write means the file is created readable only by its owner. The generated config picks SQLite in WAL journal mode, turns the built-in SSH server off, enables the LFS server, sets INSTALL_LOCK = true so the web installer never appears, and sends logs to the console. Those are defaults chosen for a small single-container server. They are only ever written once.

The half that runs every time

The second half is a single sed -i with six expressions:

bash
sed -i \
    -e "s|^HTTP_PORT *=.*|HTTP_PORT = $PORT|" \
    -e "s|^HTTP_ADDR *=.*|HTTP_ADDR = 0.0.0.0|" \
    -e "s|^ROOT_URL *=.*|ROOT_URL = $ROOT_URL|" \
    -e "s|^DOMAIN *=.*|DOMAIN = $DOMAIN|" \
    -e "s|^DISABLE_REGISTRATION *=.*|DISABLE_REGISTRATION = $NOREG|" \
    -e "s|^RUN_USER *=.*|RUN_USER = ${USER:-container}|" \
    "$CONF"

The anchors matter. Because every pattern starts with ^, a key like SSH_DOMAIN is not matched by the DOMAIN expression. A line that someone commented out is not matched either. Two limits of this approach are worth stating, because they come straight from how sed works: it knows nothing about INI sections, so it would rewrite a second HTTP_PORT line if one appeared in another section, and a line that has been deleted by hand is not restored, since sed only substitutes. For the six keys that the panel owns I accept both.

Deriving the public address

The values come from the panel's startup variables. If FORGEJO_DOMAIN is set, the script strips an optional http:// or https:// and anything after the first slash, then builds ROOT_URL as https://host/. That assumes a domain means HTTPS, which fits a panel that sits behind a reverse proxy terminating TLS. If it is empty, the script uses SERVER_IP, falls back to localhost when the allocation is 0.0.0.0, and builds http://addr:port/.

Registration defaults to closed. FORGEJO_DISABLE_REGISTRATION is read with a case that treats 1, true, TRUE and True as yes and everything else as no, with true as the default when the variable is unset. I would rather a fresh public Git server start closed and be opened on purpose.

The administrator, once

Creating the first admin is the only step that cannot be a pure function of the config file, because it changes the database. The script guards it with a marker file:

bash
if [[ ! -f "$WORK/data/.renode-admin" ]]; then
    if [[ -z "${FORGEJO_ADMIN_PASSWORD:-}" ]]; then
        say "Renode: FORGEJO_ADMIN_PASSWORD is empty, no administrator created. ..."
    elif forgejo admin user create ... --must-change-password=false >&2; then
        say "Renode: administrator ${FORGEJO_ADMIN_USER:-owner} created."
        touch "$WORK/data/.renode-admin"
    else
        say "Renode: could not create the administrator (see above)."
    fi
fi

touch runs only inside the success branch. If the password variable is empty, the server still starts and prints what to set, and the next restart after the owner fills it in creates the account. If forgejo admin user create fails, the marker is not written, so the next start tries again instead of silently leaving a server nobody can log into. I chose "start anyway, say why in the console" over exiting, because a server that is up lets the owner see logs and fix the variable.

forgejo migrate runs before that on every start, so upgrading the image tag and restarting is enough to bring the database schema forward.

The container around it

The Dockerfile downloads the official static binary for the right architecture from Codeberg and verifies it with sha256sum -c against the published checksum before installing it. It runs under tini with -g, and STOPSIGNAL SIGINT, which is how Forgejo is stopped cleanly. The entrypoint expands the panel's STARTUP variable in the same way as the parkervcp yolks, so eggs written for those behave the same.

What I took from it

The pattern I would reuse is a split by ownership. Fields that the platform owns are rewritten on every start from the platform's variables. Fields that the user owns are written once and then left alone. Seeding one-time state, like the admin account, is guarded by a marker that proves the step succeeded and not merely that it ran. Each rule is a few lines of shell, and together they make restarts boring, which is what you want from a service that someone else's repositories live in.

#bash#docker#pterodactyl#forgejo#idempotency

Keep reading

Have a project in mind?