Skip to content

Instantly share code, notes, and snippets.

@linjunpop
Last active September 11, 2024 10:21
Show Gist options
  • Save linjunpop/6247236 to your computer and use it in GitHub Desktop.
Save linjunpop/6247236 to your computer and use it in GitHub Desktop.
Deploy Rails 4 app with Dokku on DigitalOcean

Deploy Rails 4 app with Dokku on DigitalOcean

Install dokku

First create a Ubuntu 13.04 x64 droplet on DigitalOcean Control Panel

Then ssh with root account, run this in termianl:

$ wget -qO- https://raw.github.com/progrium/dokku/master/bootstrap.sh | sudo bash

Config dokku

$ cat ~/.ssh/id_rsa.pub | ssh [email protected] "gitreceive upload-key dokku"

$ git remote add dokku git@[SERVER_IP_ADDRESS]:[APP_NAME]

Deploy Rails app

Add gem 'rails_12factor' to Gemfile

Then install dokku PG plugin: https://github.com/Kloadut/dokku-pg-plugin

cd /var/lib/dokku/plugins
git clone https://github.com/Kloadut/dokku-pg-plugin postgresql
dokku plugins-install

Create app database

root@mg-dokku:~# dokku postgresql:create [APP_NAME]

Then $ git push dokku master to deploy.

When it finished, run $ docker ps -a to show status:

root@mg-dokku:~# docker ps -a
ID                  IMAGE                  COMMAND                CREATED             STATUS              PORTS
290ae95c5ae7        app/mg:latest          /bin/bash -c /start    3 minutes ago       Up 3 minutes        49153->49153
1b79bc29ae77        postgresql/mg:latest   /usr/bin/start_pgsql   6 minutes ago       Up 6 minutes        49156->5432
root@mg-dokku:~#

But we have not run database migrations yet. Let's do it:

root@mg-dokku:~# docker run -i -t app/mg /bin/bash
root@085f103bec8a:/# cd app/
root@085f103bec8a:/app# RAILS_ENV=production rake db:migrate
Your Ruby version is 1.9.3, but your Gemfile specified 2.0.0

It fails, we try another way:

root@mg-dokku:~# docker run app/mg /bin/bash -c "cd /app ; rake db:migrate"
rake aborted!
Your Ruby version is 1.9.3, but your Gemfile specified 2.0.0

Fails too. :(

Here's a workaround: dokku/dokku#156 (comment)

root@085f103bec8a:/app# export HOME=/app
root@085f103bec8a:~# for file in /app/.profile.d/*; do source $file; done
root@085f103bec8a:~# hash -r
root@085f103bec8a:~# cd /app
root@085f103bec8a:~# RAILS_ENV=production rake db:migrate
==  DeviseCreateAdmins: migrating =============================================
-- create_table(:admins)
   -> 0.0125s
-- add_index(:admins, :email, {:unique=>true})
   -> 0.0034s
-- add_index(:admins, :reset_password_token, {:unique=>true})
   -> 0.0039s
==  DeviseCreateAdmins: migrated (0.0229s) ====================================

==  CreateCustomers: migrating ================================================
-- create_table(:customers)
   -> 0.0079s
==  CreateCustomers: migrated (0.0087s) =======================================

==  AddDeviceTypeToCustomers: migrating =======================================
-- add_column(:customers, :device_type, :string, {:default=>""})
   -> 0.0068s
==  AddDeviceTypeToCustomers: migrated (0.0075s) ==============================

Then visit http://[SERVER_IP_ADDRESS]:49153

Yeah!

Config Nginx

http://wiki.nginx.org/HttpProxyModule

location / {
  proxy_pass        http://localhost:49153;
  proxy_set_header  X-Real-IP  $remote_addr;
}
@cpursley
Copy link

cpursley commented Sep 7, 2013

Thanks for the Gist.

Stupid question, but I've gotten as far as http://[MY_SERVER_IP_ADDRESS]:49153 which works and is live. But how do I set my DNS to point at this address? It won't accept a ":" character? Is there something else I need to do from the command line to configure my DNS with Dokku?

@linjunpop
Copy link
Author

Sorry @cpursley, github did not send any notification for gist comments. I think you need to config a web server(ex. nginx) to proxy request to port 49153.

@yassotreyo
Copy link

Hi,
Thanks for the gist, regarding the migration I just ran 'dokku run [APP_NAME] rake db:migrate' and it worked fine for me. The only problem I had with this : at the end, nginx doesn't serve my compiled static assets from the public directory, I think I will use S3 for that as mentioned in dokku/dokku#372.

@sangyongjung
Copy link

Hi, I used to follow the workaround for migration and found another (easier way) for that.
(https://www.digitalocean.com/community/tutorials/how-to-use-the-dokku-one-click-digitalocean-image-to-run-a-ruby-on-rails-app)
Thought you might be interested in it. :)

근데, 한국분이신가요??

@Xiangshen-Meng
Copy link

I solve the migration problem by running the "dokku run xxx rake db:migrate" from local. And I installed dokku client first. This maybe help : https://github.com/progrium/dokku/blob/master/docs/community/clients.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment