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.

FROM ruby:2.5.1RUN apt-get update -qq && apt-get install -y build-essential libpq-dev
RUN mkdir /app
WORKDIR /appCOPY Gemfile /app/Gemfile
COPY Gemfile.lock /app/Gemfile.lock
RUN bundle installCOPY . /app
source 'https://rubygems.org'
gem 'rails', '5.2.0'
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
docker-compose run linkedin.rails rails new . --api --force --database=postgresql --T
docker-compose build
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
docker-compose run linkedin.rails rake db:create
docker-compose up

That’s all. Let me know in the comments if you find any issue while following these steps.

JavaScript developer. Mostly interested in React/Redux stuffs!

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