Effortless Deployment: Launching Your Ruby on Rails App in 10 Minutes

Step-by-Step Guide Using AWS EC2 and Docker Compose for Seamless Deployment

Muralish Clinton
Level Up Coding

--

Photo by Dominik Lückmann on Unsplash

At times, deploying a Ruby on Rails app can be daunting, especially when setting up intricate configurations like web servers and dedicated application servers. However, if you’re aiming to showcase or understand the behavior of your app post-deployment without diving into complex configurations, there’s a simpler approach.

Containerization has streamlined this process, allowing you to roll out your application swiftly as a minimum viable product.

Allow me to guide you through the process for my example application, which utilizes both SQLite and MongoDB as its databases.

Step 1 — Dockerize your application

Once you’re done developing your application, start dockerizing it.

Create a dockerfile in the root of your repository

Take a look at https://docs.docker.com/get-started/02_our_app/ for further insights on containerizing a Ruby application. Below is a basic Dockerfile to initiate a Ruby on Rails application on port 3000.

# Make sure it matches the Ruby version in .ruby-version and Gemfile
ARG RUBY_VERSION=3.2.2
FROM ruby:$RUBY_VERSION
# Install libvips for Active Storage preview support
RUN apt-get update -qq && \
apt-get install -y build-essential libvips && \
apt-get clean && \
rm -rf /var/lib/apt/lists/* /usr/share/doc /usr/share/man
# Rails app lives here
WORKDIR /rails
# Set environment variables
ENV RAILS_LOG_TO_STDOUT="1" \
RAILS_SERVE_STATIC_FILES="true"
# Install application gems
COPY Gemfile Gemfile.lock ./
RUN bundle install
# Copy application code
COPY . .
# Prepare the db
RUN bundle exec rails db:prepare
# Start the server by default, this can be overwritten at runtime
EXPOSE 3000
CMD ["bundle", "exec", "rails", "server", "-e", "development", "-b", "0.0.0.0"]

Create a docker compose for your dockerfile

Create a docker-compose.yml file in the root of your application.

Docker Compose is a tool used to define and manage multi-container Docker applications. It allows you to describe a set of related services (containers) that make up your application in a YAML file. With Docker Compose, you can specify the services, their dependencies, networks, volumes, and other configurations needed to run a multi-container Docker application.

I’ve created an example docker-compose file that makes use of Mongodb.

version: '3.3'
services:
my-rails-app:
image: .
ports:
- "3000:3000"
volumes:
- .:/rails
environment:
RAILS_ENV: development
RAILS_HOST: 0.0.0.0
depends_on:
- mongo
mongo:
image: mongo
ports:
- "27017:27017"

Step 2 — Create an AWS Account

  1. Sign Up for AWS: Go to the AWS Free Tier page and create an account.
  2. Access IAM: In the AWS Management Console, go to IAM (Identity and Access Management).
  3. Create IAM User: Add a user with programmatic access. Attach policies like “AmazonEC2FullAccess.”
  4. Get Credentials: Download or copy the access key ID and secret access key for API access.
  5. Login to your IAM account

Step 3 — Create an EC2 Instance

  1. Navigate to EC2: Go to the EC2 Dashboard.
  2. Launch an EC2 Instance:
    Click on “Launch Instance.”
    Choose the Amazon Linux AMI and click “Select.”
    Select a free instance type and click “Next: Configure Instance Details.”
  3. Create a new key pair
  4. Configure network settings
  5. Configure Storage
  6. Launch Instance

Go to the instance dashboard and wait until the EC2 instance has come to Running state. Click on the instance id and connect to it using EC2 instance connect.

Step 4 — Setup Dependancies

Install environment dependencies

sudo amazon-linux-extras install docker
sudo yum update -y
sudo service docker start
sudo usermod -a -G docker ec2-user
sudo yum -y install docker-compose
sudo curl -L https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

Add SSH Key

Add ssh key of the EC2 instance to your github profile — https://docs.github.com/en/authentication/connecting-to-github-with-ssh/adding-a-new-ssh-key-to-your-github-account

Clone your repository

git clone <repo link> && cd <repo-name>

Step 5 — Run the container

Run docker-compose up -d to build the image and run the container.

Get the public IP of the EC2 instance and visit <Public IP>:3000 on your browser!

Congrats you’ve successfully deployed your ROR Application on an EC2 instance!

Conclusion

By dockerizing your application, you’ve encapsulated its dependencies, making it portable and easily deployable across environments. The use of Docker Compose further streamlines the setup, defining the services and configurations needed for your multi-container application, such as your Rails app and MongoDB.

Photo by Lala Azizli on Unsplash

This method simplifies deploying an application for demonstration or initial insights into post-deployment behavior. However, real-world deployment complexities far exceed this approach. I’ll explore a production-grade deployment in a forthcoming article, unveiling the intricacies involved.

Stay tuned for a deeper dive into deploying applications in a robust, enterprise-ready environment.

--

--