Deploying a Rails App
Rails (Ruby on Rails) is a popular web application framework written in Ruby. It emphasizes convention over configuration, rapid development, and clean code, making it a top choice for building scalable, maintainable web applications quickly.
This guide explains how to deploy a Rails application to Klutch.sh, both with and without a Dockerfile. It also covers installation and provides sample code to get started.
Prerequisites
- Ruby 3.0+
- Rails installed
- Git and GitHub account
- Klutch.sh account
Getting Started: Install Rails
- Install Rails (if not already installed):
Terminal window gem install rails - Create a new Rails app:
Terminal window rails new my-rails-appcd my-rails-app - Start the development server:
Your app should be running at http://localhost:3000.
Terminal window rails server
Sample Code (config/routes.rb)
Add a simple route to your Rails project:
Rails.application.routes.draw do root to: proc { [200, {}, ['Hello from Rails on Klutch.sh!']] }end
Deploying Without a Dockerfile
- Push your Rails app to a GitHub repository.
- Log in to Klutch.sh.
- Create a new project and give it a name.
- Create a new app:
- Select your Rails GitHub repository and branch
- Set the port to route traffic (usually 3000 for Rails)
- Choose region, compute, number of instances, and add any environment variables
- Add a start command in your app settings:
(Klutch.sh will provide the
Terminal window rails server -b 0.0.0.0 -p $PORTPORT
environment variable.) - Click “Create” to deploy. Klutch.sh will build and deploy your app automatically.
Deploying With a Dockerfile
- Add a
Dockerfile
to your project root. Example:# Use official Ruby imageFROM ruby:3.2-slim# Set working directoryWORKDIR /app# Install dependenciesCOPY Gemfile Gemfile.lock ./RUN bundle install# Copy app sourceCOPY . .# Precompile assets (if using Rails assets)RUN bundle exec rake assets:precompile# Expose port (match your Rails app)EXPOSE 3000# Start the appCMD ["rails", "server", "-b", "0.0.0.0", "-p", "$PORT"] - Push your code (with Dockerfile) to GitHub.
- In Klutch.sh, follow the same steps to create a project and app, but select the Dockerfile option when prompted.
- Set the service details and environment variables as needed.
- Click “Create” to deploy. Klutch.sh will build your Docker image and deploy your app.
Note: Your Rails app should always listen on the PORT
environment variable as shown above.
Resources
Deploying to Klutch.sh is simple and flexible. Choose the method that best fits your workflow and project requirements.