googleads
Dockerize an Existing Ruby on Rails Application | Hire ROR Developers
preloder
DevOps Docker Ruby Ruby on Rails Standard Technical

Dockerize an Existing Ruby on Rails Application

In this article, I am going to explain how to dockerize an existing Ruby on Rails application, and start & stop the application in development / production environment.

What is Docker?

Using docker, we can build and package an application or service with all the dependencies into single unit. Such a unit is called the “Docker Image”. It contains the entire source code of the application, runtime libraries and dependency software/services. Therefore it eliminates the necessity to have particular version of software packages or services pre-installed in a system for a particular application to run.
Docker enables an application to run in a system irrespective of the version and services requisites of the application deployed.
In this example, assume the application having built uses Ruby, Rails, PostgreSQL, Nginx and Unicorn.

Why we need to dockerize the application?

One main use of dockerizing is that on sharing the code of the application with other developers, they can easily setup the application in their machine / environment using simple commands instead of installing all requisite software and dependencies. We can avoid version mismatch from developer to developer.
If we have different rails applications with different versions of Ruby, Postgres, we can setup these applications without affecting other projects’ versions. The initial setup may take few minutes due to images, etc. But after that, we can get the environment running within a matter of seconds.
Also we can deploy the applications using simple steps in any kind of server and Linux distributions.

Prerequisites:

In order to dockerize, first of all, we have to install DockerEngine & DockerCompose in the laptop / instance based on the OS.
Install Docker Engine – Ref: https://docs.docker.com/engine/installation/
Install Docker Compose – Ref: https://docs.docker.com/v1.8/compose/install/
Clone your application source code from the github: (This is done in order to include the docker code in to it and then compile it as a single source code.)
$ git clone git@github.com:agiratech/your-application.git

Creating the Dockerfile

Create the “Dockerfile” file in application root path and copy the following code content into it. This file is for creating the docker mentioned in the beginning. Commit this file into your git repository.

 

Step 2: Creating the Docker Compose Configuration File

Next in our mission to dockerize, is to create the “docker-compose.yml” file in application root path and copy the following code content into it. Docker Compose will support to run one or more Docker containers. We can define everything in the configuration file and push it in to git so that other developers can use that. If you use other databases like MySql, you have to mention the mysql docker images name and its version. All the mentioned images in this article are public images hosted in dockerhub, so if you wish, you can configure with your own docker private images.

 

Create unicorn.rb file:

Next create the “unicorn.rb” file in config/containers folder and copy the following code content into it. For this rails application, we will use unicorn as our appserver.

 

Create Docker file for Nginx:

Next create the “dockerfile-nginx” file in config/containers folder and copy the following code content into it. For this rails application, we will use nginx as our web server. (For development environment this will not be required)

 

Update Database Host Name:

Modify your database host name as “host: db.local” in the database.yml file. This is configured in the compose configuration file. This will connect to the postgres which is running as the container.

Up & Running the Application:

 

The first time, this build command will take a while because it require to download all of the Docker images that we configured in the compose file for this rails application.
After that, all you require is to run docker-compose up command and the application up will be done in a few seconds. This completes our aim to dockerize the application. You can access your application in the browser with the url mentioned in your nginx conf file like http://your-domain.com/
Other developers can also just clone your recent code from github and they can run the above up & running command to setup the app for development.
Stop, Start, Down the containers:
You can stop / start the application using the below commands:

 

Stop & start will just stop & start the services, but the containers will not be down / removed. Down will stop the containers and will remove them. If we run the down command, we have to run the up command to create all containers and start it again.
We have now seen the process to successfully dockerize an existing Ruby on Rails application.
Do you want to know more about Docker? Or would you like to Dockerize your existing application or new Rails / Golang application? Please get in touch with us. Contact Us Now

The following two tabs change content below.

One thought on “Dockerize an Existing Ruby on Rails Application”

  1. Nice Article. In short description good explanation about the DevOps. Thanks For sharing the informative news.

Leave a Reply

Your email address will not be published. Required fields are marked *

[contact-form-7 id="120788" title="Web Page Form"]

Schedule Your Call