Deploy Your Rails 5 Application in China With Alibaba Cloud and Dokku

Simplify Rails deployment with Docker

Maria Schuessler
Aug 28, 2019 · 4 min read
Image for post
Photo by Martin W. Kirst on Unsplash

What is Docker?

Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and ship it all out as one package

What is Dokku?

Dokku is the love child of Docker and Heroku — the managed hosting solution created initially for Ruby on Rails. , see? With Dokku, you have the ease of deploying an application on Heroku (your application goes from development to production in 2.5 steps), but in the comfort of your own server.

This is a great solution if you don’t want to pay an arm and a leg for Dynos on Heroku, if you need to host in a specific country, or if you just think deploying with Heroku is too easy!

Why do I need Dokku for China?

The short answer is — you don’t. But it makes taking an application to production in China simpler. Heroku doesn’t have any servers in China, and in fact, the SSL provider of Heroku is blocked under the Great Firewall.

Heroku uses AWS servers to create its Ruby and Node.js buildpacks (both dependencies for Rails applications), and those are also occasionally blocked. Therefore, the only way to ensure that your application is running — and that it’s blazing fast — is to deploy directly on a Chinese server.

Before You Begin

Step 1: Buy hosting on Aliyun

The first step is purchasing hosting on ECS — Aliyun’s Elastic Compute Service. Dokku works great with both Ubuntu and CentOS. To be fully honest, Aliyun doesn’t have the greatest documentation, so for the initial server setup, the best place to go to is Digital Ocean. You can find the CentOS and Ubuntu initial setup instructions here and here.

Step 2: Install Dokku on the remote server and create the Rails application

The next step is to install the Dokku container on the server.

wget https://raw.githubusercontent.com/dokku/dokku/v0.18.1/bootstrap.shsudo DOKKU_TAG=v0.18.1 bash bootstrap.sh

The installation process takes about five to ten minutes, depending on your internet connection speed. Then, Dokku is good to go.

There are about five to six steps to actually create the application. The Dokku Documentation on this is extremely comprehensive, so I’ll link to it directly.

How to Deploy Your Rails Application With Dokku and Docker

Image for post
Image for post
Everyone can deploy with Docker! Because it only takes four steps. Photo by Adi Goldstein on Unsplash

Step 1: Unset the Proxy Environment Variable

This must be done by an admin remotely. Dokku deployments with a Dockerfile will listen to port 80 by default, so there’s no need to configure it manually.

dokku config:unset [APP_NAME] DOKKU_PROXY_PORT_MAP

Step 2: Add a Dockerfile to the application root.

Change the Ruby and Bundler versions to yours, but otherwise, the Dockerfile can be used as-is. The Dockerfile is responsible for fetching the buildpacks, and even though the first deploy will take closer to 15 minutes, all subsequent deploys will be cached, unless new gems or NPM packages are added.

FROM ruby:2.5.3# Install NodeJS and Yarn
RUN apt-get update
RUN apt-get -y install curl
RUN apt-get install -my gnupg
RUN curl -sL <https://deb.nodesource.com/setup_8.x> | bash -
RUN curl -sS <https://dl.yarnpkg.com/debian/pubkey.gpg> | apt-key add -
RUN echo "deb <https://dl.yarnpkg.com/debian/> stable main" | tee /etc/apt/sources.list.d/yarn.list
RUN apt-get update && apt-get -qqyy install nodejs yarn && rm -rf /var/lib/apt/lists/*
# Install Ruby Gems and node modules
COPY Gemfile* /tmp/
COPY package.json /tmp/
COPY yarn.lock /tmp/
WORKDIR /tmp
RUN gem install bundler -v 2.0.2
RUN bundle install --jobs 5 --retry 5 --without development test
# RUN yarn install
RUN mkdir /app
WORKDIR /app
COPY . /app
ENV RAILS_ENV production
ENV RACK_ENV production
# Execute the Procfile
CMD ["bin/run-dev.sh"]

Step 3: Add an app.json to the root with a pre-deploy and post-deploy script.

In this case, I will set it to precompile the assets before deployment and to migrate the database automatically after deployment.

{   
"name": "REPLACE WITH YOUR RAILS APP NAME",
"scripts": {
"dokku": {
"predeploy": "bundle exec rake assets:precompile",
"postdeploy": "bundle exec rake db:migrate"
}
}
}

Step 4: Add the secret key base

What’s the secret key base? Good question. The secret key is used to encrypt the session so the browser can safely send cookies to the application (i.e. for keeping the user signed in).

# this command will give a secret key. 
# run it in Terminal inside the app folder
rake secret
dokku config:set SECRET_KEY_BASE=[add output from the previous command]

git push dokku master

Thank you for reading this article!

Better Programming

Advice for programmers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store