Setting Up Docker for Ruby on Rails 7

JetThoughts
JTWay
Published in
3 min readMar 11, 2024

Introduction:

Docker has become an indispensable tool for modern software development, offering a consistent environment and simplifying setup across different systems. In this guide, we’ll walk through the process of configuring Docker for Ruby on Rails 7, enhancing workflow efficiency and collaboration.

Prerequisites:

Before we begin, ensure you have Docker installed on your system. Follow the instructions below based on your operating system:

For example, to install Docker on Ubuntu, run the following commands in the terminal:

sudo apt-get update
sudo apt-get install docker.io
sudo service docker start

Then, download the prepared repository:

git clone https://github.com/Qj7/rails_7_with_docker

Setting Up Dockerfile:

The Dockerfile serves as the blueprint for building the Docker image for our Rails application.
It's essential to use only trusted or official base images to ensure the reliability and security of your Docker environment.

Avoid:

FROM random-source-from-internet/ruby:3.1.4

Instead, opt for:

FROM ruby:3.1.4

If you have your own image registry, you can leverage it as follows:

# Assuming that is your own image registry, which you control entirely
FROM your-own-registry.com/ruby:3.1.4

Alternatively, if you have your own image registry: How to use your own registry.

While our current configuration utilizes Ruby version 3.1.4, you have the flexibility to select any Ruby version of your preference. Simply update the version in both the `Gemfile` and `Dockerfile`. Below is an example template:

# Assuming that is your own image registry, which you control entirely
FROM your-own-registry.com/ruby:3.1.4Dockerfile# Use the official Ruby image as the base image
FROM ruby:3.1.4
# Set the working directory inside the container
WORKDIR /app
# Install dependencies
RUN apt-get update && \
apt-get install -y nodejs && \
gem install bundler
# Copy Gemfile and Gemfile.lock to the working directory
COPY Gemfile Gemfile.lock ./
# Install gems
RUN bundle install
# Copy the rest of the application code
COPY . .
# Expose port 3000 to the outside world
EXPOSE 3000
# Start the Rails server
CMD ["rails", "server", "-b", "0.0.0.0"]

Multi-container Setup:

For a comprehensive application setup, we often require additional services like databases. Let’s demonstrate setting up PostgreSQL alongside our Rails application.

Creating Docker Compose Configuration:

Docker Compose simplifies managing multi-container Docker applications. Here’s a sample docker-compose.yml for our Rails and PostgreSQL application:

yamlversion: '3.8'
services:
# Service for the Ruby on Rails web application
web:
build: .
command: bundle exec rails s -p 3000 -b '0.0.0.0'
ports:
- "3000:3000"
volumes:
- .:/app
depends_on:
- db
environment:
DATABASE_URL: postgres://postgres:postgres@db:5432/rails_7_with_docker
# Service for the PostgreSQL database
db:
image: postgres:13
ports:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
# Define a volume for storing PostgreSQL data
volumes:
postgres_data:

Running Docker Containers:

With the Dockerfile and Docker Compose configuration in place, build and run the Docker containers using the following commands:

docker-compose build
docker-compose run web rails db:create
docker-compose up

Accessing the Rails Application:

Once the containers are up and running, access your Rails application by navigating to http://localhost:3000 in your web browser.

Conclusion:

In this guide, we’ve explored how Docker transforms Ruby on Rails 7 development workflows. By containerizing our Rails application and leveraging Docker Compose for multi-container setups, we ensure consistency, simplify collaboration, and enhance productivity. Embrace Docker in your development journey to streamline your Ruby on Rails projects. Happy coding!

--

--

JetThoughts
JTWay
Editor for

Ruby on Rails | React | Vue | We make apps. Find out more at jtway.co