Rails is the popular and versatile Web application framework written in Ruby that provides default structures for a database, a Web service and Web pages. While Rails can be used as an API, the view logic and additional middleware can make it a bit of an overkill.
Fortunately, integrated into the Rails 5 release is the Rails API gem. Rails API is a lightweight alternative to Rails for building applications that don’t require full Rails functionality. In this tutorial on the Codeship blog, Charles Wang explains how to build microservices using the Rails API gem and containerised application packaging platform Docker.
Due to some setup issues with Docker in Rails 5, which is still in beta, Wang discusses the build using a Rails 4 setup with the Rails API gem added as a dependency. The tutorial is aimed at Mac users and begins with setting up the container’s environment using the Dockerfile. He then sets up the Docker Compose tool to handle communication between the Rails API app and the MySQL container.
With the relevant gems added to the Gemfile, the author provides the sample code to create the Docker image, as well as the Rails API app structure. After this, simply configure the database and run the Web server to check for the Rails status page.
Once the app is up and running, followers can begin adding data models and controllers, then generate communication between several similar applications using JSON over HTTP by exposing different Web server ports on the same Docker host.