November 22, 2013

Beginner

How To Use the Dokku One-Click DigitalOcean Image to Run a Ruby on Rails App

Introduction


A significant hurdle in developing an application is providing a sane and easy way to deploy your finished product. Dokku is a Platform as a Service solution that enables you to quickly deploy and configure an application to a production environment on a separate server.

Dokku is similar to Heroku in that you can deploy to a remote server. The difference is that Dokku is built to deploy to a single, personal server and is extremely lightweight. Dokku uses Docker, a Linux container system, to easily manage its deployments.

In this guide, we will cover how to deploy a Ruby on Rails app with Dokku using the DigitalOcean Dokku one-click installation image.

Step One –– Create the Dokku Droplet


The first thing we need to do is create the VPS instance that contains our Dokku installation. This is simple to set up using the DigitalOcean Dokku application.

Click on the "Create" button to create a new droplet:

DigitalOcean create droplet

Name your droplet, and select the size and region that you would like to use:

DigitalOcean configure droplet 1

Scroll down and click on the "Applications" tab. Select the Dokku application image:

DigitalOcean Dokku image

Select your SSH keys if you have them available. If you do not already have them configured, now is a great time to create SSH keys to use with your DigitalOcean droplets. This step will help you later.

Click "Create Droplet". Your Dokku VPS instance will be created.

DigitalOcean final create

Once your droplet is created, you should set up your domain name to point to your new Dokku droplet. You can learn how to configure domain names with DigitalOcean here.

Step Two –– Access the Droplet To Complete Configuration


You can complete your Dokku configuration by accessing your VPS from a web browser.

If you configured a domain name to point to your Dokku installation, you should visit your domain name with your favorite web browser. If you do not have a domain name configured, you can use your droplet's IP address.

You will be given a simple configuration page. There are a few parts that you need to configure here.

DigitalOcean Dokku ssh keys

First, check that the Public Key matches the computer that you will be deploying from. This means that if your project is on your home computer, you should use the public key that corresponds to that set up.

If you selected multiple SSH keys to embed during droplet creation, only the first will be available here. Modify it as necessary.

DigitalOcean Dokku hostname configuration

Next, modify the Hostname field to match your domain name. Leave this as your IP address if you do not have a domain name configured.

Choose the way that you want your applications to be referenced. By default, the application will be served like this:

http://your_domain.com:app_specific_port_number

If you select the "Use virtualhost naming for apps" check box, your apps will be accessible using a virtualhost instead:

http://app_name.your_domain.com

Click the "Finish Setup" button to complete the configuration.

Step Three –– Deploy a Sample Ruby on Rails Application to your Dokku Droplet


Now that we have Dokku configured, we can begin to work on the application that we want to deploy. For the most part Ruby on Rails applications should work as expected, especially if they have been optimized for Heroku.

Install the PostgreSQL Database Plugin


We will actually begin on the Dokku droplet that we just deployed. Log into the droplet as root.

Dokku handles database resources through a plugin system. If your application needs a database, you need to install the plugin and create a database in that way.

Change to the plugins directory:

cd /var/lib/dokku/plugins

We will clone the PostgreSQL plugin to this directory with git:

git clone https://github.com/Kloadut/dokku-pg-plugin.git postgresql

Now, we need to issue the Dokku command that builds the plugins:

dokku plugins-install

This will install and configure the necessary components to provide Postgres access to our applications. If we issue the Dokku help command, we can see that we now have new commands available:

dokku help
. . .
plugins         Print active plugins
postgresql:create <app>     Create a PostgreSQL container
postgresql:delete <app>     Delete specified PostgreSQL container
postgresql:info <app>       Display database informations
postgresql:link <app> <db>  Link an app to a PostgreSQL database
postgresql:logs <app>       Display last logs from PostgreSQL container
run <app> <cmd>                                 Run a command in the environment of an application
url <app>                                       Show the URL for an application
. . .

The next step is actually creating a database that our app can plug into and use. As you can see above, this is easy with the new plugin commands that we have available:

dokku postgresql:create app_name

It is important that you choose the app_name carefully here. It must be the same name that you plan to use when deploying your application. You will be accessing the application through:

http://app_name.domain.com

Create a Basic Rails Application


From your development computer (the one with the SSH key that matches the one you entered during the Dokku installation), you need to install Ruby and Rails. A good way to do this is with RVM, the Ruby version manager.

You can install Ruby on Rails with RVM on an Ubuntu droplet here.

We will also download a few things that are needed for our Rails application:

sudo apt-get install nodejs postgresql-server-dev-all

Now, we will create a new, simple blog with Rails. We can do that easily by typing:

rails new blog

This will create a new Rails application in a directory called blog in the current directory.

Change into the directory:

cd blog

We can now use Rails scaffolding abilities to generate some components for our blog:

rails generate scaffold post title:string body:text

We need to migrate the database created by the scaffolding into our application:

rake db:migrate

Finally, we need to tell Rails which page to use as the default when visiting the site. Open the config/routes.rb file with your text editor:

nano config/routes.rb

You need to add a line that specifies the default file to server by adding the root route (in red):

Stuff::Application.routes.draw do
  root 'posts#index'
  resources :posts
  . . .
  . . .
end

Save and close the file.

It is important to test your blog on the current computer before trying to deploy to Dokku. Type this into your terminal to start the rails development server:

rails s

In a web browser, visit the IP address of the computer you are developing on, followed by :3000.:

your_dev_ip_address:3000

You should see a bare-bones Rails blog:

Rails development blog

Back in the terminal, exit out of the rails server by holding the control key and hitting 'C':

CTRL+C

Now that we have verified that our application is functioning correctly, we need to make a few modifications to make our app work with Dokku.

First, the Rails development environment uses an SQLite database to manage its information, but Dokku requires you to deploy with Postgres. We can change this by modifying the Gemfile in the application directory.

nano Gemfile

We need to comment out the SQLite requirement and add the Postgres requirement:

source 'https://rubygems.org'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.0.1'

# Use sqlite3 as the database for Active Record
# gem 'sqlite3'
gem 'pg'

# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.0'
. . .

Save and close the file.

Make the project aware of these changes by issuing this command:

bundle install

Initiate Version Control Through Git


Now, we need to add our project to git in order to push it to Dokku.

If you do not have git installed, you will need to do that now. If your development machine is an Ubuntu machine, you can install using:

sudo apt-get install git

You need to configure some global settings so that git will allow you to add commits:

git config --global user.email "your_email"
git config --global user.name "your_name"

Now, initialize a git repository in the root application directory:

git init

Add all of the project's files and commit them:

git add .
git commit -m 'Initial commit'

Push the App to Dokku and Deploy


To deploy to Dokku, you simply need to add the domain name as a remote of your git repository:

git remote add remote_name dokku@your_domain.com:app_name

The remote_name can be anything that you'd like to refer to your Dokku machine. In most cases dokku is a good choice. The app_name must match the name you chose when you created your PostgreSQL database on the Dokku droplet.

Now, to deploy, just push your app to Dokku:

git push remote_name master

Dokku will deploy your app and give you a URL where you can reach your application:

-----> app_name linked to redis/app_name container

-----> app_name linked to postgresql/app_name database
-----> Deploying app_name ...
-----> Cleaning up ...
=====> Application deployed:
       http://app_name.your_domain.com

To dokku@your_domain.com:app_name
 * [new branch]      master -> master

There is one last step that we need to take before your app will work correctly. Because our application is using a new database in a new environment, we need to re-run the rake db:migrate command within our application environment.

We can do this by SSHing into our Dokku droplet. Dokku allows you to pass commands directly to an application environment.

Change into the application directory and then issue the command:

cd /home/dokku/app_name

Note: Due to a change in the PostgreSQL plugin, you now must link the database to your application explicitly by issuing a command that looks like:

dokku postgresql:link app_name database_name

Afterwards, we can migrate the database like this:

dokku run app_name rake db:migrate

You should now be able to visit your application at:

http://app_name.your_domain.com

Dokku deployed rails app

Conclusion


You should now have a working Rails application deployed and running on your Dokku droplet. You can learn more about Dokku by visiting the project's page on GitHub. We also have a section of other Dokku tutorials at DigitalOcean.

By Justin Ellingwood

Try this tutorial on an SSD cloud server.

Includes 512MB RAM, 20GB SSD Disk, and 1TB Transfer for $5/mo! Learn more

Create an account or login:

Share this Tutorial

Vote on Hacker News

38 Comments

Write Tutorial
  • Gravatar johnjameswhitman 4 months

    I kept getting an error while precompiling assets on the deploy: rake aborted! could not connect to server: Connection refused Is the server running on host "127.0.0.1" and accepting TCP/IP connections on port 5432? ... ! Precompiling assets failed. This happens because Rails will initialize the app and try connecting to its database before its configured during the build/deploy process. Adding the following configuration into application.rb fixed the issue for me: config.assets.initialize_on_precompile = false Ref: https://github.com/progrium/dokku/issues/165, https://github.com/progrium/dokku/issues/202

  • Gravatar harvey.d.garrett 4 months

    I'm getting the same error with Rails 4 which doesn't support 'config.assets.initialize_on_precompile'. Any ideas?

  • Gravatar johnjameswhitman 4 months

    One option is to compile your assets locally and add them to the Git repo being pushed up to Dokku: https://devcenter.heroku.com/articles/rails-asset-pipeline#compiling-assets-locally. If Heroku sees a manifest-.json file it will skip the assets:precompile step assuming you've already done it ahead of time--not sure if Dokku would do the same.

  • Gravatar hakala.onni 4 months

    Couldn't use this on my droplet (512mb). Tried multiple times but always had response: Action Did Not Complete

  • Gravatar Clifton Labrum 4 months

    While it appears that Dokku handles much of the domain stuff, do you still need to add a CNAME of app_name and point it at @ in the Digital Ocean Control Panel?

  • Gravatar Clifton Labrum 4 months

    Everything in this tutorial has gone well for me until right at the end when I try to do the db:migrate. Anyone else seeing this error? user@host:/home/dokku/app# dokku run app rake db:migrate rake aborted! cannot load such file -- tasks/rails /app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:229:in `require' /app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:229:in `block in require' /app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:214:in `load_dependency' /app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:229:in `require' /app/Rakefile:12:in `' I'm on Rails 4 and Ruby 2.

  • Gravatar Kamal Nasser 4 months

    @Clifton: Yes, you can either do that or create a wildcard CNAME record: Name: * Hostname: @

  • Gravatar edward.verenich 4 months

    i seem to be having the same problem with trying to run db:migrate

  • Gravatar Jonathan Solomon 4 months

    Is there any way to run a rails console on the server when using this deployment method?

  • Gravatar Jonathan Solomon 4 months

    i.e., I tried logging into SSH on root and running "dokku run rails c" and I get what looks to be rails console, but I cannot interact with it.

  • Gravatar Kamal Nasser 4 months

    @Jonathan: Have you tried dokku run app-name rails c?

  • Gravatar Jonathan Solomon 4 months

    @Kamal Yes, tried that. I'll be more specific: If I run ​dokku run myrailsapp rails c​​, substituting myrailsapp for the name my application, I do get a rails console like so: Loading production environment (Rails 4.0.0) irb(main):001:0> However... If I try and get Modelname.last at that console, for example, nothing happens.

  • Gravatar ollieglaskovik 4 months

    After following these instructions, my app didn't seem to be using a migrated database, i.e. it behaved as if the migrations hadn't been run. To check the database's state, I installed the command line postgres client (with apt-get install postgresql-client), but couldn't get it to connect. Could you advise how to connect to the local database? Then after rebooting, the rails app didn't come back up. Dokku reports it has change port; running dokku url shows it's new port. But nothing is being served at that url. Any idea how to get it back up?

  • Gravatar Kamal Nasser 4 months

    @Jonathan: Hmm, that's odd. Try creating an issue: https://github.com/progrium/dokku/issues I've briefly searched in the closed issues section and found nothing regarding the rails console.

  • Gravatar Emanuel 4 months

    i have a postgres db on my local, if i follow the above instruction for dokku postrgres create , how do i migrate or link the db?

  • Gravatar Kamal Nasser 4 months

    @Emanuel: It has to be of the same name as your app so that it can be linked to it.

  • Gravatar olivier 3 months

    How can we access logs or some kind of info. I deployed successfully but web server not responding. Where are the apps deployed on server?

  • Gravatar Kamal Nasser 3 months

    @olivier: Run

    docker ps
    and once you know what your container's ID is, run
    docker logs containerId

  • Gravatar james 3 months

    It asks me to update docker when running `dokku postgresql:create app_name` so I update following directions [here](http://docs.docker.io/en/latest/installation/upgrading/) and docker updates to `Docker version 0.7.6, build bc3b2ec` Then `git push dokku master` just hangs.

  • Gravatar ibnesayeed 3 months

    I have the same issue as @james described above.

  • Gravatar santiagoherrero000 2 months

    I have the same issue after updating Docker. I update following this directions: https://www.digitalocean.com/community/articles/how-to-use-the-digitalocean-docker-application Any idea how to make it work again? Thanks!

  • Gravatar bradley.m.allen 2 months

    Mine also hangs when pushing, I updated docker to the latest and it hanged. I then did a fresh install with dokku then updated dokker version by version until it created my DB, still hangs. Same problem as @james, @ibnesayeed and @santiagoherrero000

  • Gravatar golodhros 2 months

    Same thing here! It hangs when pushing. @kamal, any solution? Could you guys review this process? If not, this post is not useful anymore. Thanks

  • Gravatar Denis Sergeev 2 months

    Same problem as @james, @ibnesayeed, bradley.m.allen and @santiagoherrero000

  • Gravatar alex.hartley 2 months

    I had the git push hanging problem. Ended up using a vanilla 13.04 ubuntu setup and installing dokku manually. Works fine for me now.

  • Gravatar jellingwood 2 months

    We're in the process of updating our Dokku image to the latest stable. It should address the issues many of you have been seeing and should be available shortly.

  • Gravatar grimm 2 months

    Very strange; I've pushed to my dokku but it doesn't give me a url at all. When I ssh into the dokku user I have I see a folder named my app but can't rake db:migrate as it doesn't have the proper files: root@skilldate:/home/dokku/skilldate# la branches config description hooks nginx.conf PORT URL cache CONTAINER HEAD info objects refs and when I try to hit my main url / domainname or my appname.domain.com - I get a 502 bad gateway.. very confused.

  • Gravatar jellingwood 2 months

    The Dokku image has been updated to version 0.2.1. Please try again if you were having trouble with the old image.

  • Gravatar golodhros 2 months

    I have tried again, and although the hang is not happening anymore but instead the deployment is duplicating the app name, so it deploys to app_name.app_name.hostname.com I also see that 502 bad gateway.

  • Gravatar jellingwood about 1 month

    @golodhros: What are the exact git commands you are using to add the remote repository and to push?

  • Gravatar golodhros about 1 month

    @jellingwood I used: git remote set-url dokku [email protected]:miv and the results of git remote -v are: dokku [email protected]:miv (fetch) dokku [email protected]:miv (push)

  • Gravatar Kamal Nasser about 1 month

    @golodhros: What command are you using to push the app?

  • Gravatar Benjamin Coppock about 1 month

    Any ideas for importing data from a postgres sql dump? I've tried `cat backup.sql | dokku postgresql:create my_database` and though I get no errors, I also don't get any data loaded from the backup.sql file.

  • Gravatar golodhros about 1 month

    @kamal git push dokku master

  • Gravatar grimm about 1 month

    I just rebuilt with the new image and after following all the steps again, now when I attempt to push I get fatal 'domain' does not appear to be a git repository. fatal. Could not read from remote repository. Please make sure you have the correct access rights and the repository exists. I've got my dns set properly (skilldate); I've followed the steps, created the db etc. Don't think the app (also named skilldate and set up as virtual hosting with skilldate.skilldate.com ) -- can't do the initial push.

  • Gravatar golodhros about 1 month

    After struggling a lot with this tutorial, I have been able to deploy my app. I just can give you guys one advice: don't call your Dokku App the same way you called your Rails App.

  • Gravatar shane.r.drury about 1 month

    I'm getting a 502 bad gateway error by following the tutorial exactly: https://www.digitalocean.com/community/articles/how-to-use-the-dokku-one-click-digitalocean-image-to-deploy-a-python-flask-app Any ideas?

  • Gravatar berkeley 23 days

    Any updates? Have these issues been resolved? Hesitant to spend too much time on this unless it's known to work.

Leave a Comment

Create an account or login:
Ajax-loader