502 bad gateway error when deploy rails app with dokku on digitalocean

Follow this how-to-use-the-dokku-one-click-digitalocean-image-to-run-a-ruby-on-rails-app guide, i got nginx error – ‘502 bad gateway’.

How to solve?

first, check app logs to see what happen.

dokku logs <your-app-name>

I got DATABASE_URL error. it seems dokku didn’t link my app with PostgreSQL.

Checkout the PostgreSQL database_url:

dokku postgresql:info <your-app-name>

Host: xxxxxx
Port: 49154
User: 'root'
Password: '*********'
Database: 'db'

Url: 'postgres://root:*******@xxxxx:49154/db'

now link this url with my app:

dokku postgresql:link <your-app-name> <your-db-name>

or

dokku config:set <your-app-name> DATABASE_URL='postgres://root:*******@xxxxx:49154/db'

git push again, got db:migration error!

rake manually according to this gist:

docker run -i -t app/mg /bin/bash

cd app/
export HOME=/app
for file in /app/.profile.d/*; do source $file; done
hash -r
cd /app
RAILS_ENV=production rake db:migrate

DONE! push again, it works.