Spree Commerce

Try It Now

Novel Ways of Deploying Spree Featuring Dokku

Posted on April 24, 2014 by Bruno Buccolo

Dokku: The Smallest PaaS Implementation

Dokku is a new tool to deploy apps that has been getting a lot of attention lately. According to Jeff Lindsay, creator of Dokku and avid open source contributor, Dokku is the smallest PaaS implementation you’ve ever seen. After an easy setup on any host, you’ll have git push deployments and component based architecture. This post will explain how to test it out with a fresh Spree install.

Getting a Fresh Spree Install

This section will cover how to setup a clean Spree install. If you want to skip this process, just clone buccolo/spree_dokku then head over to Setting up Dokku .

The process is simple. We start by installing Rails, which will create a new Rails project.

$ gem install rails -v 4.0.4
$ rails new spree
$ cd spree/

Now add some goodies to the Gemfile:

gem 'pg'
gem 'rails_12factor'
gem 'spree', github: 'spree/spree', branch: '2-2-stable'
gem 'spree_auth_devise', github: 'spree/spree_auth_devise', branch: '2-2-stable'

Bundle up and add some sample data. Also pre-compile your assets to avoid a Dokku bug

$ bundle
$ bundle exec rails g spree:install --migrate=true --sample=true --seed=true
$ bundle exec rake assets:precompile

Now, start Rails and take a moment to admire your new Spree store. You can learn more about how to start using Spree in our Getting Started Guide

Everything is set, now we’ll need a Dokku server to deploy this to.

Setting up Dokku

Dokku is pretty easy to install. To make things even faster, we’ll use Digital Ocean, because it provides an Ubuntu application image with Dokku ready to go. Look for the image Dokku-v0.2.1 on Ubuntu 13.04. It’s recommended to use a server with at least 1GB of RAM, so you don’t swap.

After the server has been bootstrapped, visit the IP address on the browser to set-up Dokku. You can either configure DNS right know by pointing it to your new server, or manually configure the /etc/hosts in your machine so it looks good and you save yourself some trouble:

If you haven’t already, create a git project and commit your Spree store:

$ git init
$ git add .
$ git commit -am "Ready for Dokku"

Now, I promised you git push deploys a la Heroku, so let’s have it:

$ git remote add dokku dokku@sudokku.io:spree
$ git push dokku
Counting objects: 5, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 484 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
----- Building spree ...
       Ruby app detected

...

===== Application deployed:
       http://spree.sudokku.io

Behind the curtains Dokku is creating a new Docker container and installing our app on it. Notice how we just added a new remote to our git repo and pushed. A Dokku server can serve many apps, making it a really good tool to deploy simple apps. If Ruby is not your thing, no problem; Dokku also supports NodeJS, Java, Scala, Python, Clojure and others!

Now let’s check our app in production, shall we?

Aww :(

What happened? Let’s use Dokku itself to debug, but first a quick alias to speed things up:

$ alias dokku='ssh -t root@sudokku.io dokku'

$ dokku logs spree
(erb):9:in `rescue in <main>': Invalid DATABASE_URL (RuntimeError)
        from (erb):6:in `<main>'

Right! We haven’t even configured a database yet! The Dokku way of solving this is by installing a Postgres Plugin. Now not only will we be able to deploy apps effortlessly, we’ll also be able to create Postgres containers. Installing a plugin is easy:

$ ssh root@sudokku.io

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

Now that we’ve installed the plugin, we can just run some commands to have a Postgres container created for us. The plugin also makes it easy to ‘connect’ a database container to our app container: by calling postgresql:link, the plugin will set DATABASE_URL environment variable and reload our app.

$ dokku postgresql:create spree-pg
$ dokku postgresql:link spree spree-pg
----- Setting config vars and restarting spree
DATABASE_URL: postgres://root:1234@172.17.42.1:49160/db
----- Releasing spree ...
----- Release complete!
----- Deploying spree ...
----- Checking status of PostgreSQL
      Found image postgresql/spree database
      Checking status... ok.
----- Deploy complete!

----- spree linked to postgresql/spree-pg database

Our database is ready. All we have to do now is run the migrations and create the admin user. For the sake of the example, we’re also setting an environment variable to demonstrate how easy it is and to skip configuring SSL: just to log into our app.

$ dokku run spree bundle exec rake db:migrate
$ dokku run spree bundle exec rake spree_auth:admin:create
$ dokku config:set spree RAILS_ENV=development

Whenever you change an environment variable, Dokku takes care of restarting your app. So you should be able to head over to the app to see it live. The screenshot below shows Spree’s admin screen:

Conclusion

Dokku is a pretty exciting tool that makes it really easy to deploy apps and attach services on-demand. You can see by the version number (0.2.1) that it might be too early to use it for complex scenarios. After all, it’s not fair to compare it to other large hosting providers, especially because the hosted PaaS solutions shine when it comes to reliability and scalability.

Further Reading