My personal experience Dockerizing a Ruby on Rails Application: Deploy to AWS ECR using Github Actions
I recently received a request on how to Dockerize a Ruby on Rails application after a user reviewed the article I wrote on Dockerizing a Rust Application. Containerizing a Ruby on Rails application enhances scalability, consistency, and dependency isolation across environments, while also improving deployment efficiency, making it an essential tool for modern development practices.
Ruby on Rails is a reliable and powerful framework for developing web applications, boasting a rich ecosystem and strong community support. The combination of Ruby on Rails and Docker allows developers, to build, deploy and scale web application with enhanced speed and reliability.
Docker ensures that your application runs consistently across all the environments, it eliminates the popular statements from developers. “The application works on my system, i am not sure why it is not working on your system or i am not sure why it is not working on production.”
Setting up Ruby on Rails and Docker provides a solid foundation for building and deploying web applications. Rails offers a powerful framework for development, while Docker ensures your applications run consistently across all environments. Together, they create a streamlined and efficient workflow for modern web development.
This article shows how to Dockerize your first application in Ruby on Rails and deploy this application to AWS ECR. This is an assumption that you already have an understanding of AWS ECR and GitHub Actions.
Below is a step-by-step guide on how to Dockerize an application in Ruby on Rails and deploy this application to AWS ECR.
Step 1: Setting Up Your Environment: Install Ruby on Rails, Docker and Docker-Compose
Ruby on Rails requires Ruby to be installed first. You need to have Ruby, Docker Compose and Docker running in your development environment if you do not already have it running.
- Ruby: Install Ruby follow this link, it explains how to install Ruby on different Operating systems or environments.
- Docker: Install Docker and Docker Compose, follow this link, it describes how to install Docker on different Operating System. Next steps will be to install Rails once you have successfully installed Ruby.
Use the command below to install Rails.
gem install rails
To verify the installation of Rails.
rails -v
Step 2: Setting Up the Project. Todo App on Ruby on Rails
Next, create a New Rails application which will be Todo App. Run the following command below.
rails new todo-app -d postgresql
cd todo-app
After running the command a folder called todo-app will be created which will contain the Rails code. Also, the -d postgresql flag will set PostgreSQL as the default database.
Step 3: Set Up the Database Configuration
After running the command with the flag above, the database configuration will be created in this location config/database.yml which will contain information about the database connection string.
default: &default
adapter: postgresql
encoding: unicode
host: <%= ENV['DATABASE_HOST'] %>
username: <%= ENV['DATABASE_USER'] %>
password: <%= ENV['DATABASE_PASSWORD'] %>
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
database: todo_app_development
test:
<<: *default
database: todo_app_test
production:
<<: *default
database: todo_app_production
Step 4: Install Gems
Run the command below to install all the required gems.
what is Gems
bundle install
Step 5: Generate the Todo Scaffold
Generate a basic scaffold code for the Todo app with the command below
rails generate scaffold Todo title:string completed:boolean
Step 6: Migrate the Database
Run the migration command below to setup the database schema
rails db:create db:migrate
Step 7: Create the Dockerfile
In order to containerize the Ruby on Rails Application, you need to create a Dockerfile . Create a new file on the root directory of your project with filename, Dockerfile which will contain instructions on how to build the Docker image of your application.
Open the newly created file Dockerfile and copy and paste the code below in the file.
# Use an official Ruby runtime as a parent image
FROM ruby:3.2.2
# Install dependencies
RUN apt-get update -qq && apt-get install -y nodejs postgresql-client
# Set the working directory
WORKDIR /app
# Copy the Gemfile and Gemfile.lock into the container
COPY Gemfile Gemfile.lock /app/
# Install gems
RUN bundle install
# Copy the rest of the application code
COPY . /app
# Precompile assets for production
RUN RAILS_ENV=production bundle exec rake assets:precompile
# Expose port 3000 for the Rails app
EXPOSE 3000
# Start the Rails server
CMD ["rails", "server", "-b", "0.0.0.0"]
Step 8: Create a .dockerignore File
In order to prevent unnecessary files from being added to your Docker image, create a new file in the root directory with filename dockerignore . By adding the .dockerignore file this will reduce the size of the docker image and only ensure that we have only the necessary files.
Open the newly created file .dockerignoreand copy and paste the code below in the file. You can add more file that you will like to ignore, this is just an example.
log/*
tmp/*
node_modules/*
.git
Step 9: Set Up Docker Compose
Using Docker Compose will simplify the process of running the Ruby on Rails application and it’s dependencies such as the PostgreSQL database. This will allow you to conveniently parse the database connection string.
Create a file with the filenamedocker-compose.yml on the root directory. Copy and paste the code below in the newly created docker-compose.yml file.
version: '3.8'
services:
db:
image: postgres:13
volumes:
- db_data:/var/lib/postgresql/data
environment:
POSTGRES_PASSWORD: password
POSTGRES_USER: postgres
POSTGRES_DB: todo_app_development
web:
build: .
command: bash -c "rm -f tmp/pids/server.pid && rails server -b 0.0.0.0"
volumes:
- .:/app
ports:
- "3000:3000"
depends_on:
- db
environment:
DATABASE_HOST: db
DATABASE_USER: postgres
DATABASE_PASSWORD: password
DATABASE_NAME: todo_app_development
volumes:
db_data:
Step 10: Run the Ruby on Rails Application with Docker Compose
The command below will build the Docker image, start the PostgreSQL database and run the Rails server.
docker-compose up
You can access the Ruby on Rails application from your favorite browser at http://localhost:3000