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
gem 'rails', '5.2.0'
Step 3: Create an empty Gemfile.lock
Step 4: Create a docker-compose.yml
command: bundle exec rails s -p 3000 -b '0.0.0.0'
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
Step 7: Replace the contents of
config/database.yml with the following:
Step 8: Create the database
docker-compose run linkedin.rails rake db:create
Step 9: Boot your Rails app
That’s all. Let me know in the comments if you find any issue while following these steps.