Guide
March 24th

Docker Ruby on Rails tutorial. Dockerize RoR app in 3 minutes.

This is a basic docker ruby on rails tutorial that presents how you can run your 'Hello World'-style Ruby on Rails application on dockerize platform.

Prerequisites

Before you install Rails, you should check to make sure that your system has the proper prerequisites installed. These include Ruby and SQLite3.

Open up a command line prompt and verify that you have a current version of Ruby installed:

$ ruby -v
ruby 2.6.5

Rails requires Ruby version 2.5.0 or later. If the version number returned is less than that number, you'll need to install a fresh copy of Ruby. To install ruby it's recommended to use rbenv or rvm.

You will also need an installation of the SQLite3 database. To verify that you have installed it just run the following command in your prompt:

$ sqlite3 --version

Get Started

Let's create a Ruby on Rails project and then see how we can dockerize it using Docker.

Step 1 - Install Rails

To install Rails, use the gem install command provided by RubyGems:

$ gem install rails

Then, to verify that you have everything installed correctly, you should be able to run the following:

$ rails --version

If it says something like Rails 6.0.0, you are ready to continue.

Step 2 - Generate a new Rails Application

Rails comes with a number of scripts called generators that are designed to make your development life easier by creating everything that's necessary to start working on a particular task. So just use it.

$ rails new tutorial

header image

The tutorial project directory has a number of auto-generated files and folders that make up the structure of a Rails application. Most of the work in this tutorial will happen in the app folder.

Step 3 - Run the app

You actually have a functional Rails application already. To see it running, you need to start a web server. You can do this by running the following command in the tutorial directory:

$ cd tutorial
$ rails server

header image

This will fire up Puma, a web server distributed with Rails by default. To see your application in action, open a browser window and navigate to http://localhost:3000. You should see the Rails default information page:

header image

This "Welcome aboard" page is the smoke test for a new Rails application: it makes sure that you have your software configured correctly enough to serve a page.

Step 4 - Dockerize the Project

Setup Docker

Before creating a container for the Django application and shipping it off, you need to install Docker on your local machine. For learning purpose, you will install Docker Community Edition. Select your OS from the list below and follow the setup instructions:

Make the docker App image

The next stage to improve your docker RoR workflow is adding a Dockerfile to your project. The structure of a Dockerfile can be considered a series of instructions on how to build your container/image.

Start the Dockerfile by creating an empty file named Dockerfile in the root of your project. Then, complete each instruction according to the following example:

# Dockerfile
# gets the docker parent image
FROM ruby:2.6.5

RUN apt-get update && apt-get install -y npm && npm install -g yarn

RUN mkdir -p /var/app
COPY . /var/app
WORKDIR /var/app

RUN bundle install

CMD rails s -b 0.0.0.0

The first line fetches a ruby docker image called "ruby:2.6.3-slim" from Docker Hub. This image is called the base image. It has a lightweight operating system called "slim", with ruby 2.6.3 pre-installed. The next command installs dependencies for Rails. Images have their own "folder structure" inside. With the follow commands, we create the '/var/app' folder and make that the 'default' folder. We copy our RoR project there and then run bundle install. This installs all the required gems inside the image. We then proceed with copying the entire application into the image.

Building and Running the Container

Building the docker ruby on rails container is very straight forward once you have Docker and Docker Machine on your system. The following command will look for your Dockerfile and download all the necessary layers required to get your container image running. Afterwards, it will run the instructions in the Dockerfile and leave you with a container that is ready to start.

To build your ruby on rails docker container, you will use the docker build command and provide a tag or a name for the container, so you can reference it later when you want to run it. The final part of the command tells Docker which directory to build from.

$ docker build -t ror-tutorial .

The final step is to run the container you have just built using Docker:

$ docker run -it -p 3000:3000 ror-tutorial

The command tells Docker to run the container and forward the exposed port 3000 to port 3000 on your local machine. After you run this command, you should be able to visit http://localhost:3000 in your browser.

If you have some problems with libraries version (see the example below), you can relax the correct packages and versions from the variable check_yarn_integrity on development section located on config/webpacker.yml only for this tutorial purpose.

header image

NOTE: If you need to apply this workaround, you will need to build the image again.

You can see the Docker containers that are currently running on your system (along with their Container IDs) with:

$ docker ps -a

To turn off your Docker container, run:

$ docker stop container_id

Push to cloud

1. Create your app

In order to install your RoR project, just create a new dockerize ruby app via cli or admin panel and set a port to 3000.

header image

2. Push your docker container

Given your RoR application created on dockerize platform, you need to get the deployed host to set up on rails. For this example this host is: ror-tutorial.on.dockerize.io.

Rails 6 has a new feature where only whitelisted hosts are allowed to be accessed. By default only localhost is permitted. You can read mode about it on Blocked host on Rails 6.

So you need to set up the app host. For this add the following line on config/environments/development.rb file:

# config/environments/development.rb
config.hosts = "ror-tutorial.on.dockerize.io"

Then just build your image and upload it to the platform.

header image

3. Set up resources

header image

4. Logs and app status

header image

5. Release your app

After to click on the release button, your php docker tutorial will be deployed, Just click on the generated URL and you will get your app running.

header image

Now you can deploy your Ruby on Rails app without a massive build time.

Bonus 1: SSL certificate for HTTPS

It's already done for you. If you need to connect your custom domain, SSL certificate will be provided for it.


Bonus 2: Autoscaling

With autoscaling the app will be scaled up when CPU and RAM load goes up and scaled down when it goes down.

Now you can deploy your Ruby on Rails app without a massive build time.

Dockerize your Job

Just for $2/month
Share this article:

Ask a question

Please type your name.
Please provide your email address.
Please tell us a bit more

More great guides

Docker nodejs express tutorial

Docker NodeJS Express guide

Simple Nodejs Socket guide for docker deploy. Use it for comfort start with us.

Read Guide
Docker python django tutorial

Docker Python Django tutorial

This is a brief tutorial that presents how you can run a simple 'Hello World'-style web application written in Django in docker.

Read Guide
Docker PHP laravel tutorial

Docker PHP laravel tutorial

This quickstart guide provides a basic introduction to the Laravel framework running on docker. This is a great starting point if you are brand new to the Laravel framework or if you want to have your docker PHP app.

Read Guide
7 times faster deployment
Dockerize automaticaly completes deployment after receiving your Docker image
50% of AWS price
We don’t have any hidden prices and will never ask you to pay extra fee for our services
Open Sidebar Close Sidebar