wtqlca
Last Updated: February 25, 2016
·
1.33K
· jvanbaarsen

Relink postgresql to rails after dokku reboot

I really love Dokku in combination with the dokku-pg-plugin. The only problem im having with it, is that after a server reboot, the database link with the rails app will be gone.

This has to do with the fact that you can't be sure about the IP and port given to you by docker.

If you ever run into a situation, in which you had to restart your server, and the rails app wont run afterwards, try the following:

  • 1. Put the following in a file called relinkdb
#!/bin/bash
APP="$1";
CURRENT_URL=`dokku config:get $APP DATABASE_URL`

dokku postgresql:create $APP
ID=$(docker ps -a | grep "postgresql/$APP":latest |  awk '{print $1}')
IP=$(docker inspect $ID | grep IPAddress | awk '{ print $2 }' | tr -d ',"')
NEW_URL=$(echo $CURRENT_URL | sed -e "s/@.*\//@$IP\//g")
dokku config:set $APP DATABASE_URL=$NEW_URL
  • 2. run the command chown +x relinkdb
  • 3. run the executable with: ./relinkdb APPNAME

This will re-create the Postgresql container for Docker, and link the container to your rails app.