Database
gaigenticai
HOBBYOP

9 months ago

I have a project in local which is deployed in docker and it creates a DB automatically. So when i deploy this code to railway via github will the db also be created automatically based on my files?

Solved$10 Bounty

Pinned Solution

alexwebgr
HOBBY

9 months ago

you can always use to the database that is created from the postgres service and connect to it by passing the DATABASE_URL

and just remove it from your script, is there a specific reason that it has to be created by the sh script?

for a rails project i would normally add this at the end of the Dockerfile

ENTRYPOINT ["/rails/bin/docker-entrypoint"]

CMD ["bundle", "exec", "foreman", "start", "-f", "Procfile"]

and inside the docker-entrypoint, i run the command that will either set up the db for the first time and run the migrations. you can adjust it to use your use case

#!/bin/bash -e

echo "prepare db"
# If running the rails server then create or migrate existing database
if [ "$1" == "bundle" ] && [ "$2" == "exec" ] && [ "$3" == "foreman" ] && [ "$4" == "start" ]; then
  ./bin/rails db:prepare
  echo "prepared db"
fi

exec "${@}"

5 Replies

alexwebgr
HOBBY

9 months ago

As long as you have provisioned a Database service and can connect to that server it should be fine. When does the database gets created? While deploying the app or when running it?


alexwebgr

As long as you have provisioned a Database service and can connect to that server it should be fine. When does the database gets created? While deploying the app or when running it?

gaigenticai
HOBBYOP

9 months ago

In docker it's a single sh file which creates everything. Now inrailway we need to create nodes and connect with the main service. I was hoping if there is a way to create the DB when we create a service and connect via GitHub, based on the docker file, a postgress service as well?


alexwebgr
HOBBY

9 months ago

you can always use to the database that is created from the postgres service and connect to it by passing the DATABASE_URL

and just remove it from your script, is there a specific reason that it has to be created by the sh script?

for a rails project i would normally add this at the end of the Dockerfile

ENTRYPOINT ["/rails/bin/docker-entrypoint"]

CMD ["bundle", "exec", "foreman", "start", "-f", "Procfile"]

and inside the docker-entrypoint, i run the command that will either set up the db for the first time and run the migrations. you can adjust it to use your use case

#!/bin/bash -e

echo "prepare db"
# If running the rails server then create or migrate existing database
if [ "$1" == "bundle" ] && [ "$2" == "exec" ] && [ "$3" == "foreman" ] && [ "$4" == "start" ]; then
  ./bin/rails db:prepare
  echo "prepared db"
fi

exec "${@}"

alexwebgr
HOBBY

9 months ago

feel free to mark this answer as Accepted if you found it helpful pray emoji

thanks

alex


sim
FREE

9 months ago

When your project is created, you will need to add a database service to it - https://docs.railway.com/guides/build-a-database-service


Status changed to Solved chandrika 9 months ago


Loading...