Introduce
There are many different ways to deploy a Rails application, and there are many platforms and tools that support this. One of those ways is to deploy the rails application on a server with Dokku installed that I often use for small and medium customers at TakeIT Vietnam. This article will provide a brief overview of Dokku and how to easily deploy a Rails application to a server with Dokku installed.
What is Dokku?
If you must briefly like Dokku, you can describe Dokku as an open source Platform as a Service project that replaces Heroku. Dokku leverages the power of Docker to be deployed on a wide range of hardware and on low-cost servers. You have complete control over your server and enjoy the same outstanding features as Heroku. In addition, Dokku supports a lot of plugins to help you deploy more applications.
Deploying a Rails App
Deploying a Rails application to the Dokku server is simple. First, you will need to install CLI Dokku, which is a command line interface for managing and deploying applications. Once installed, you can use the CLI to deploy your application by creating a Dokku application and then pushing your code onto the application. Let’s go step by step.
- Minimum configuration requiredA newly installed operating system according to the list below:
The server has one of the following architectures:
- AMD64 (alternatively known as
x86_64
), commonly used for Intel cloud servers - ARMV7 (alternatively known as
armhf
), commonly used for Raspberry PI - ARMV8 (alternatively known as
arm64
), commonly used for Raspberry PI and AWS Graviton
To avoid server overload and unexpected errors during use:
- Server needs at least 1GB of Ram
- If the server is less than 1GB Ram then create a swap for your server :1234567891011<span class="token builtin class-name">cd</span> /var<span class="token function">touch</span> swap.img<span class="token function">chmod</span> <span class="token number">600</span> swap.img<span class="token function">dd</span> <span class="token assign-left variable">if</span> <span class="token operator">=</span> /dev/zero <span class="token assign-left variable">of</span> <span class="token operator">=</span> /var/swap.img <span class="token assign-left variable">bs</span> <span class="token operator">=</span> 1024k <span class="token assign-left variable">count</span> <span class="token operator">=</span> <span class="token number">1000</span><span class="token function">mkswap</span> /var/swap.img<span class="token function">swapon</span> /var/swap.img<span class="token function">free</span><span class="token builtin class-name">echo</span> <span class="token string">"/var/swap.img none swap sw 0 0"</span> <span class="token operator">>></span> /etc/fstab
- If the server is less than 1GB Ram then create a swap for your server :
Finally a domain name, this is optional. However, using a domain name makes your application more accessible than just an IP address.
- AMD64 (alternatively known as
- Install Dokku on the serverInstall Dokku using the bootstrap file123<span class="token function">wget</span> https://dokku.com/install/v0.29.4/bootstrap.sh <span class="token comment">#0.29.4 là phiên bản cài đặt</span><span class="token function">sudo</span> <span class="token assign-left variable">DOKKU_TAG</span> <span class="token operator">=</span> v0.29.4 <span class="token function">bash</span> bootstrap.sh
Set up SSH keys and VirtualHost
12345678910<span class="token comment"># usually your key is already available under the current user's `~/.ssh/authorized_keys` file</span><span class="token function">cat</span> ~/.ssh/authorized_keys <span class="token operator">|</span> dokku ssh-keys:add admin<span class="token comment"># you can use any domain you already have access to</span><span class="token comment"># this domain should have an A record or CNAME pointing at your server's IP</span>dokku domains:set-global dokku.me<span class="token comment"># you can also use the ip of your server</span>dokku domains:set-global <span class="token number">10.0</span> .0.2 - Deploying a Rails applicationAfter completing the installation of Dokku on the server, we will start deploying a basic rails application by following these steps:
In this example, we will use the Heroku Ruby on Rails “Getting Started” app repo for deployment.
Clone source code from git repository to server.
1234<span class="token comment"># from your local machine</span><span class="token comment"># SSH access to github must be enabled on this host</span><span class="token function">git</span> clone https://github.com/heroku/ruby-getting-startedCreate an app on Dokku
123<span class="token comment"># on the Dokku host</span>dokku apps:create ruby-getting-startedBy default, Dokku will not provide pre-installed databases that we have to install through Plugins. You can access the list of available Plugins from the Dokku homepage , here I will be using Postgres so I will install the Postgres plugin provided from Dokku
12345<span class="token comment"># on the Dokku host</span><span class="token comment"># install the postgres plugin</span><span class="token comment"># plugin installation requires root, hence the user change</span><span class="token function">sudo</span> dokku plugin:install https://github.com/dokku/dokku-postgres.gitCreate a new database
123<span class="token comment"># create a postgres service with the name railsdatabase</span>dokku postgres:create railsdatabaseLink the previously created application with the newly created database.
By linking the application and the database, Dokku will create an environment variable of
DATABASE_URL
on the Dokku application.1234<span class="token comment"># on the Dokku host</span><span class="token comment"># each official datastore offers a `link` method to link a service to any application</span>dokku postgres:link railsdatabase ruby-getting-startedDeploy the
ruby-getting-started
app to the Dokku server. All you need to do is add the remote to git and push the source code to the Dokku git repository.123456<span class="token comment"># from your local machine</span><span class="token comment"># the remote username *must* be dokku or pushes will fail</span><span class="token builtin class-name">cd</span> ruby-getting-started<span class="token function">git</span> remote <span class="token function">add</span> dokku dokku@dokku.me:ruby-getting-started<span class="token function">git</span> push dokku main:masterAfter running git push dokku main:master command, you will get ouput almost like below.
1234567891011121314151617181920212223Counting objects: <span class="token number">231</span> , done.Delta compression using up to <span class="token number">8</span> threads.Compressing objects: <span class="token number">100</span> % <span class="token punctuation">(</span> <span class="token number">162</span> /162 <span class="token punctuation">)</span> , done.Writing objects: <span class="token number">100</span> % <span class="token punctuation">(</span> <span class="token number">231</span> /231 <span class="token punctuation">)</span> , <span class="token number">36.96</span> KiB <span class="token operator">|</span> <span class="token number">0</span> bytes/s, done.Total <span class="token number">231</span> <span class="token punctuation">(</span> delta <span class="token number">93</span> <span class="token punctuation">)</span> , reused <span class="token number">147</span> <span class="token punctuation">(</span> delta <span class="token number">53</span> <span class="token punctuation">)</span>----- <span class="token operator">></span> Cleaning up <span class="token punctuation">..</span> .----- <span class="token operator">></span> Building ruby-getting-started from herokuish <span class="token punctuation">..</span> .----- <span class="token operator">></span> Adding BUILD_ENV to build environment <span class="token punctuation">..</span> .----- <span class="token operator">></span> Ruby app detected----- <span class="token operator">></span> Compiling Ruby/Rails----- <span class="token operator">></span> Using Ruby version: ruby-2.2.1----- <span class="token operator">></span> Installing dependencies using <span class="token number">1.9</span> .7Running: bundle <span class="token function">install</span> --without development:test --path vendor/bundle --binstubs vendor/bundle/bin -j4 --deploymentFetching gem metadata from https://rubygems.org/ <span class="token punctuation">..</span> <span class="token punctuation">..</span> <span class="token punctuation">..</span> <span class="token punctuation">..</span> <span class="token punctuation">..</span> .Fetching version metadata from https://rubygems.org/ <span class="token punctuation">..</span> .Fetching dependency metadata from https://rubygems.org/ <span class="token punctuation">..</span>Using rake <span class="token number">10.4</span> .2<span class="token punctuation">..</span> .<span class="token operator">==</span> <span class="token operator">==</span> <span class="token operator">=</span> <span class="token operator">></span> Application deployed:http://ruby-getting-started.dokku.me
summary
Installing a Rails application on a Dokku server is a great way to quickly and easily deploy an application. With a few commands, you’ve got your application up and running in no time. In addition to the simple commands above, Dokku can do more with advanced configurations, plugins provided by Dokku and the community also make Dokku more flexible and extensible.
Dokku has advantages but at the same time also has disadvantages, hope you will like and wait for upcoming Dokku related articles.