Experience
Projects
My Stack
Case studies
Contact me
Open my CV

·6 min read

A Ruby image for Pterodactyl that reads database.yml with awk to decide on db:prepare

A Rails app on a game-server panel gets started by a script, not by a person at a terminal, so the script has to decide on its own whether the app has a database to prepare. Run bin/rails db:prepare on an app with no database configured and the start fails. Skip it on an app that has one and the tables may never get created. My Ruby start script for Pterodactyl had to tell those two apart without asking the user, so it reads config/database.yml with a small awk program.

Project
Pterodactyl yolks
Date
Oct 2026
Stack
Docker, Bash, Ruby, Rails, Pterodactyl
Commit
e028013

The job of the image

The yolks repo builds Docker images for Pterodactyl, where a user uploads a project to a container and presses start. The Ruby image is built per Ruby minor from the official ruby:<minor>-slim-bookworm base and adds the headers native gems need: libpq-dev, default-libmysqlclient-dev, libsqlite3-dev, libyaml-dev and a few more. The interesting part is ruby-start, the script that decides what "start this project" means. Its header documents the order:

code
RUBY_START set:      bundle install, then that command.
bin/rails present:   Rails server (db:prepare when there is a database to
                     prepare, assets:precompile when the app has assets).
config.ru present:   rackup (or puma when rackup is not in the bundle).
app.rb present:      ruby app.rb.

Every branch ends with a message that tells the user what to do next, for example when config.ru exists but neither rackup nor puma is in the bundle: add gem "rackup" to the Gemfile.

Gems must survive a restart

Before any of that, the container filesystem matters. The official Ruby image points GEM_HOME and BUNDLE_APP_CONFIG at /usr/local/bundle, outside the volume Pterodactyl keeps. So the Dockerfile moves them under the home directory and keeps a project's own .bundle/config honoured:

dockerfile
ENV USER=container HOME=/home/container \
    GEM_HOME=/home/container/.gem \
    BUNDLE_APP_CONFIG=.bundle \
    PATH=/home/container/.gem/bin:/usr/local/bundle/bin:$PATH

The script sets BUNDLE_PATH to vendor/bundle inside the project, so a restart installs only what changed, and in production it exports BUNDLE_WITHOUT=development:test unless the user set their own. Parallelism for bundle install is nproc, capped at 4.

Should it run db:prepare

Here is the decision. A fresh Rails 8 app keeps its production data in SQLite files under storage/, so db:prepare is right. An app generated with rails new --minimal leaves the production section commented out, and db:prepare there is wrong. I did not want a flag the user has to remember, so the script looks for evidence:

bash
env_db=$(awk -v env="$RAILS_ENV" '
    $0 ~ "^" env ":" { in_env = 1; next }
    /^[^[:space:]#]/ { in_env = 0 }
    in_env && /^[[:space:]]+database:/ { print "yes"; exit }
' config/database.yml 2>/dev/null)
if [ -n "${DATABASE_URL:-}" ] || [ -n "$env_db" ]; then
    bin/rails db:prepare >&2 || { say "db:prepare failed."; exit 1; }
fi

It is a three-state machine. A line starting with the environment name and a colon turns in_env on. Any later line that begins in column one with something other than whitespace or # turns it off, which is how the next top-level key, such as test:, ends the section. While inside, the first indented database: line prints yes and exits. A commented-out # production: does not match the opening pattern because the line starts with #, and the # also keeps commented lines from closing a block early.

What I actually ran

I tested the awk by hand rather than trusting it. With RAILS_ENV=production and a file whose production: section has <<: *default and a database: storage/production.sqlite3 line, it printed yes. With a second file where production: and its database: line are both commented out and the section only inherits *default, it printed nothing, so the script skips db:prepare in that case. That is only two inputs. I did not run it on a multi-database Rails config, where production: has nested primary: and queue: blocks with the database: key indented a level deeper. The pattern ^[[:space:]]+database: accepts any indentation, so it should say yes for those, but I have not confirmed it, so I will not claim it.

Two other facts shape the design. The awk is deliberately dumb: it does not expand ERB, so a database: value built from <%= ENV[...] %> still counts as a database, which is the behaviour I want, since the question is whether one is configured and not what it is called. And DATABASE_URL short-circuits the whole check, because Rails prefers it anyway.

Assets and a guard rail

The same style of evidence decides asset compilation. The helper in_bundle greps Gemfile.lock for a gem at the right indentation:

bash
in_bundle() { grep -qs "^    $1 " Gemfile.lock; }

If production and any of propshaft, sprockets or sprockets-rails is in the lockfile, the script runs bin/rails assets:precompile. An API-only app with none of them skips a step that would only fail. Four spaces and a trailing space in the pattern matter: they match a top-level gem entry in the lockfile's specs: list and not a dependency line two levels deeper, and not a gem whose name merely starts with the same letters.

Before booting, the script also warns when RAILS_ENV is production and none of SECRET_KEY_BASE, RAILS_MASTER_KEY or config/master.key exists, saying that Rails will refuse to boot. It warns and then continues, because I cannot see the user's environment well enough to be sure they are wrong.

Everything on stderr

One rule runs through the script: say writes to stderr, and so do the redirected bundle, db:prepare and precompile commands. The header comment gives the reason, that some entrypoints capture the startup command's stdout and execute it. If a progress line went to stdout, an entrypoint could try to run "Installing gems" as a command. That is the same trap I ran into in the BEAM image, and I now treat stdout as reserved for whatever the final exec produces.

What I took from it

A startup script that guesses is worse than one that asks, but a script that asks is worse than one that checks. Reading three files, Gemfile.lock, config/database.yml and the environment, answers questions the user would otherwise have to answer in a panel setting. The limit is honesty about coverage: the awk handled the two shapes I tried, and the follow-up worth doing is a test with a multi-database config before anyone relies on it.

#ruby#rails#docker#bash#pterodactyl

Keep reading

Have a project in mind?