kamal icon indicating copy to clipboard operation
kamal copied to clipboard

Execute arbitrary command (db setup/migration) when setting up the application for the first time

Open IvanPavlovskiAtPW opened this issue 3 months ago • 1 comments

So i ran in an interesting issue here, before I deploy my app i need to run a command that sets up the db. I tried doing this in the pre-app-boot hook, but the problem there is that the env is still not copied up until that point, the hook fails.

I can do this in post hook, but I can't get there since my app won't get healthy unless the db is setup.

I don't see a clear solution here.

You could say that the problem is in the app itself, it could setup the db when starting, but this is an open source app which I'm not maintaining, is there a solution here ?

It would be great if we could just copy the env before running the hook. Currently I solved this by running the setup command without any hooks (it fails, since app won't get healthy), but this did copy the env. Then i run setup again, now with a pre hook, which works correctly since the env is copied.

IvanPavlovskiAtPW avatar Sep 18 '25 13:09 IvanPavlovskiAtPW

The default rails install sets this up in the docker-entrypoint file in your bin directory

#!/bin/bash -e

# Enable jemalloc for reduced memory usage and latency.
if [ -z "${LD_PRELOAD+x}" ]; then
    LD_PRELOAD=$(find /usr/lib -name libjemalloc.so.2 -print -quit)
    export LD_PRELOAD
fi

# If running the rails server then create or migrate existing database
if [ "${@: -2:1}" == "./bin/rails" ] && [ "${@: -1:1}" == "server" ]; then
  ./bin/rails db:prepare
fi

exec "${@}"

joshleblanc avatar Sep 28 '25 15:09 joshleblanc