9 steps for dockerizing a Rails api-only application

Docker is a program that helps us in containerizing the environment in which development in being done. It helps us in creating a virtualized environment in which we can development applications without interfering with the host machine.
You can download and install Docker by following the official documentation.
In this article, we’ll go through the process of containerizing a Rails api-only application. This article will be part of a series in which we’ll be building the APIs for a job portal application. The code will be available in this repository.
The official documentation does a pretty good job of getting started with creating a Ruby on Rails containerized application. Feel free to reference that source of information as well if you need to.
Step 1: Create a Dockerfile
FROM ruby:2.5.1RUN apt-get update -qq && apt-get install -y build-essential libpq-dev
RUN mkdir /appWORKDIR /appCOPY Gemfile /app/Gemfile
COPY Gemfile.lock /app/Gemfile.lockRUN bundle installCOPY . /app
Step 2: Create a Gemfile
source 'https://rubygems.org'
gem 'rails', '5.2.0'
Step 3: Create an empty Gemfile.lock
Step 4: Create a docker-compose.yml
version: '3'
services:
linkedin.rails.postgres.db:
image: postgres
volumes:
- ./tmp/db:/var/lib/postgresql/data
linkedin.rails.web:
build: .
command: bundle exec rails s -p 3000 -b '0.0.0.0'
volumes:
- .:/myapp
ports:
- "3000:3000"
depends_on:
- linkedin.rails.postgres.db
Step 5: Build the project
docker-compose run linkedin.rails rails new . --api --force --database=postgresql --T
Step 6: Re-build the project due to changes in Gemfile
docker-compose build
Step 7: Replace the contents of config/database.yml
with the following:
default: &default
adapter: postgresql
encoding: unicode
host: linkedin.rails.postgres.db
username: postgres
password:
pool: 5
development:
<<: *default
database: myapp_development
test:
<<: *default
database: myapp_test
Step 8: Create the database
docker-compose run linkedin.rails rake db:create
Step 9: Boot your Rails app
docker-compose up
That’s all. Let me know in the comments if you find any issue while following these steps.