Deploying a Rails Application to Dokku Server
Introduction
There are many different ways to deploy a Rails application, with many platforms and tools to help with this. One of those ways is to deploy a Rails application to a server that has Dokku installed. This article will provide a brief overview of Dokku and how to easily deploy a Rails application to a server that has Dokku installed.
What is Dokku?
If forced to describe Dokku in a few words, it can be described as an open source Platform as a Service (PaaS) alternative to Heroku. Dokku leverages the power of Docker to be able to deploy on various hardware and on cheap servers. You can fully control your server and enjoy features similar to Heroku. Additionally, Dokku supports many plugins to help you deploy a variety of applications.
Deploying a Rails Application
The process of deploying a Rails application to a Dokku server is very simple. First, you will need to install the Dokku CLI, 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 app and then pushing your code to the app. Let’s go step by step.
1. Minimum Configuration Requirements
A new operating system is installed according to the list below:
The server has one of the following architectures:
- AMD64 (also known as
x86_64
), commonly used for Intel cloud servers. - ARMV7 (also known as
armhf
), commonly used for Raspberry PI. - ARMV8 (also known as
arm64
), commonly used for Raspberry PI and AWS Graviton.
To avoid server overload and unexpected errors during use:
- The server needs at least 1GB of RAM
- If the server has less than 1GB of RAM, create a swap for your server:
cd /var touch swap.img chmod 600 swap.img dd if=/dev/zero of=/var/swap.img bs=1024k count=1000 mkswap /var/swap.img swapon /var/swap.img free echo "/var/swap.img none swap sw 0 0" >> /etc/fstab
Finally, a domain name, this is not mandatory. However, using a domain name will make it easier for your application to access than an IP address.
2. Set up Dokku on the server
Install Dokku using the bootstrap file.
wget https://dokku.com/install/v0.29.4/bootstrap.sh #0.29.4 là phiên bản cài đặt sudo DOKKU_TAG=v0.29.4 bash bootstrap.sh
Setting up SSH key and Virtualhost.
# usually your key is already available under the current user's `~/.ssh/authorized_keys` file cat ~/.ssh/authorized_keys | dokku ssh-keys:add admin # you can use any domain you already have access to # this domain should have an A record or CNAME pointing at your server's IP dokku domains:set-global dokku.me # you can also use the ip of your server dokku domains:set-global 10.0.0.2
3. Deploying a Rails Application
After setting up Dokku on the server, we will start deploying a basic Rails application following these steps:
In this example, we will use the Heroku Ruby on Rails “Getting Started” app repo to deploy.
Clone the source code from the git repository to the server.
# from your local machine # SSH access to github must be enabled on this host git clone https://github.com/heroku/ruby-getting-started
Create an application on Dokku.
# on the Dokku host dokku apps:create ruby-getting-started
By default, Dokku will not provide any pre-installed databases and we have to install them through Plugins. You can access the list of available Plugins from Dokku’s homepage, here we will use Postgres so we will install the Postgres Plugin provided by Dokku.
# on the Dokku host # install the postgres plugin # plugin installation requires root, hence the user change sudo dokku plugin:install https://github.com/dokku/dokku-postgres.git
Create a new database
# create a postgres service with the name railsdatabase dokku postgres:create railsdatabase
Linking the previously created application with the newly created database.
By linking the application and database, Dokku will create an environment variable DATABASE_URL
on the Dokku application.
# on the Dokku host # each official datastore offers a `link` method to link a service to any application dokku postgres:link railsdatabase ruby-getting-started
Perform the deployment of 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.
# from your local machine # the remote username *must* be dokku or pushes will fail cd ruby-getting-started git remote add dokku dokku@dokku.me:ruby-getting-started git push dokku main:master
After running the command git push dokku main:master, you will receive output similar to the one below.
Counting objects: 231, done. Delta compression using up to 8 threads. Compressing objects: 100% (162/162), done. Writing objects: 100% (231/231), 36.96 KiB | 0 bytes/s, done. Total 231 (delta 93), reused 147 (delta 53) -----> Cleaning up... -----> Building ruby-getting-started from herokuish... -----> Adding BUILD_ENV to build environment... -----> Ruby app detected -----> Compiling Ruby/Rails -----> Using Ruby version: ruby-2.2.1 -----> Installing dependencies using 1.9.7 Running: bundle install --without development:test --path vendor/bundle --binstubs vendor/bundle/bin -j4 --deployment Fetching gem metadata from https://rubygems.org/........... Fetching version metadata from https://rubygems.org/... Fetching dependency metadata from https://rubygems.org/.. Using rake 10.4.2 ... =====> 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 simple commands, you will have your own application running in no time. In addition to the simple commands mentioned above, Dokku can also do many more things with advanced configurations, plugins provided by Dokku, and the community also makes Dokku more flexible and expandable.
Dokku has its advantages but also has its disadvantages, hopefully you will like it and look forward to the upcoming articles related to Dokku.
QuanTDA1
10 February, 2023Interesting article!