Dive into the exciting world of Docker Compose and Rails with my comprehensive step-by-step guide
In this captivating article, we’ll lead you through the seamless process of setting up a Ruby on Rails application using the power of Docker Compose. Imagine effortlessly defining and running multi-container Docker applications - it’s like magic!”
Define the project
In this article, we will guide you through the process of setting up a Ruby on Rails application using Docker Compose. Docker Compose allows you to define and run multi-container Docker applications easily. We’ll walk you through creating the necessary files and configurations to build and run a Rails app within a Docker environment.
1. Introduction to Docker Compose
Docker Compose is a tool that simplifies the process of defining, managing, and running multi-container Docker applications. It uses a simple YAML file to configure services, networks, and volumes for your application, making it easy to set up and manage your development environment.
2. Setting up the Dockerfile
The first step is to create a Dockerfile
in your project directory. The Dockerfile
is a script that specifies the base image and additional dependencies needed for your Rails app.
# syntax=docker/dockerfile:1
FROM ruby:3.1.2
RUN apt-get update -qq && apt-get install -y nodejs postgresql-client
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
# Configure the main process to run when running the image
CMD ["rails", "server", "-b", "0.0.0.0"]
3. Creating the Gemfile
Now, create a bootstrap Gemfile
with the necessary dependencies for your Rails application.
source 'https://rubygems.org'
gem 'rails'
4. Configuring docker-compose.yml
The docker-compose.yml
file describes the services that make up your app, how to get their Docker images, and the configuration needed to link them together.
services:
db:
image: postgres
volumes:
- ./tmp/db:/var/lib/postgresql/data
environment:
POSTGRES_PASSWORD: password
web:
build: .
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- .:/myapp
ports:
- "3000:3000"
depends_on:
- db
5. Building the Rails Project
With the Dockerfile
and docker-compose.yml
in place, you can now generate the Rails skeleton app using Docker Compose:
docker compose run --no-deps web rails new . --force --database=postgresql
If you encounter the error (defined?(@source) && @source) || Gem::Source::Installed.new
, try using the command docker compose build
.
6. Handling File Ownership
If you are running Docker on Linux, the files created by rails new
may be owned by root. Change the ownership of the new files with:
sudo chown -R $USER:$USER .
Now, rebuild the Docker image again:
docker compose build
7. Connecting the Database
By default, Rails expects a database running on localhost
. However, we need to point it at the db
container. Modify the contents of config/database.yml
as follows:
default: &default
adapter: postgresql
encoding: unicode
host: db
username: postgres
password: password
pool: 5
development:
<<: *default
database: myapp_development
test:
<<: *default
database: myapp_test
8. Starting and Stopping the Application
To boot the app, use:
docker compose up
To stop the application, use:
docker compose down
9. Conclusion
Congratulations! You have successfully set up a Ruby on Rails application using Docker Compose. This approach enables a consistent and portable development environment, making it easier for your team to collaborate.
10. FAQs
Q1: What is Docker Compose?
A1: Docker Compose is a tool that simplifies the process of defining and running multi-container Docker applications.
Q2: Why do I need to modify the config/database.yml
file?
A2: By default, Rails expects a database on localhost
, but we need to point it to the db
container.
Q3: Can I use Docker Compose for production deployments?
A3: While Docker Compose is primarily for development, you can use similar concepts with Docker Swarm or Kubernetes for production.