Skip to content

Instantly share code, notes, and snippets.

@joshteng
Last active May 12, 2022 14:38
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save joshteng/895ba92a281c0d477089 to your computer and use it in GitHub Desktop.
Save joshteng/895ba92a281c0d477089 to your computer and use it in GitHub Desktop.
Using Dokku to deploy a Rails Application

#Goal Deploy your Rails App super easily with Dokku on Digital Ocean cheap cheap!

##Notes

  • Follow 12 factor design (include the rails_12factor gem)
  • Don't forget your Procfile with the command to start up your application server
  • I prefer using external hosted logging services like Logentries (not in this guide)
  • Set up performance monitoring AppSignal or New Relic (not in this guide)

##step 1 at digitalocean.com
Spin up a new VPS with Dokku (from experience, I suggest using at least 1GB if you are running both your database and application servers in one Droplet or you run out of memory pretty soon!)

##step 2 tunneled into server
SSH into your server and run apt-get update

##step 3 tunneled into server

  1. add postgres with following this guide: https://github.com/Kloadut/dokku-pg-plugin
  2. run dokku plugins-install if you haven't already
    *if there're issues while trying to install plugins because of some nginx errors, uninstall nginx apt-get remove nginx and reinstall apt-get install nginx (or any other package for that matter)

##step 4 tunneled into server except # 2 done locally in your application codebase

  1. create database dokku postgresql:create <app_name>
  2. change database.yml to use url environment variable url: <%= ENV['DATABASE_URL'] %>
  3. run dokku postgresql:link <app_name> <database_name> (this will link the postgres running on one docker container with my rails app in another docker container by simply configuring the env variable).

##step 5 in browser

  1. go to server IP address in browser and set SSH key and app naming url

##step 6 done in your local console

  1. add dokku as git remote (use ip address if don't want to set root domain as A record) git remote add dokku dokku@<your_ip_address_or_root_domain>:<app_name> see instructions if having trouble: https://github.com/progrium/dokku
  2. cat ~/.ssh/id_rsa.pub | ssh root@<your_ip_address> "sudo sshcommand acl-add dokku dokku"
  3. git push dokku master

##step 7 tunneled into server
On the server, run:

dokku run <app_name> rake db:migrate
dokku run <app_name> rake db:seed
dokku config:set <app_name> SOME_VARIABLE=SOMEVALUE ANOTHER_VARIABLE=ANOTHER_VALUE

https://github.com/progrium/dokku#environment-variable-management

##step 8 in browser

  1. point your subdomain to your Droplet (VPS)'s IP address (DNS A Record)! (you can choose to point your root domain to your server as well. I don't do this because I usually run my applications on a subdomain and a regular HTML info site on the root domain)
  2. test in browser! <app_name>.rootdomain.com

##Reference

##Questions I have I'm pretty new to DevOps and stuff (thanks to Heroku).
I've deployed Wordpress apps in the past and used Monit to monitor my daemon processes such as MySQL and Apache.
How would you do it with Docker? If you know something this please leave a comment below!

@joshteng
Copy link
Author

You can restart a docker container like this:
docker restart <container_id>

p.s. you can find your container id this way docker ps

@joshteng
Copy link
Author

If you restarted your the docker container running your database, I've found that dokku postgresql:link <app_name> <database_name> does not work very well.

Instead you should run docker ps to see which port your database container is running on and then manually set your app's environment variable by doing this:
dokku config:set <app_name> DATABASE_URL=<database_url> (this should automatically restart your app as well. if not run dokku deploy <app_Name>)

As long as your DATABASE_URL environment variable is correct (ie. same port number, correct db name: db etc+), it should work.

Hint:
Your DATABASE_URL should look something like this: postgres://<db_username>:<db_password>@<docker_gateway>:<port_number>/db

You can find your postgresql docker container's gateway by doing this docker inspect <container_id>. And you should find a line like this: "Gateway": "172.17.42.1",. so your docker_gateway would be 172.17.42.1

note: unlike a typical server set up, your postgres is not technically running on localhost(0.0.0.0), instead, it's running within it's own docker container and each docker container has own internal IP address. to learn more: https://docs.docker.com/userguide/dockerlinks/

@joshteng
Copy link
Author

Dokku has been amazing to help me get started very quickly. But just like any frameworks, it's too much of an abstraction to understand how things really works. I would still want to use Docker on its own when I'm not trying to catch deadlines.

@joshteng
Copy link
Author

This has been very helpful for me when learning more about Docker http://www.youtube.com/watch?v=mVN7aTqr550

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