Ruby on Rails - Using Docker with Rails
Introduction
Docker is a powerful tool that allows you to containerize your applications, providing consistency across different environments. Using Docker with a Rails application ensures that it runs the same way in development, testing, and production. This guide will cover how to use Docker with a Rails application.
Key Points:
- Docker provides consistency across different environments.
- Containerizing a Rails application simplifies deployment and scaling.
- This guide covers the basics of using Docker with a Rails application.
Setting Up Docker
To get started with Docker, you need to install Docker on your machine. Follow the instructions on the Docker website to install Docker for your operating system.
Creating a Dockerfile
A Dockerfile is a script that contains instructions for building a Docker image. Here is an example Dockerfile for a Rails application:
# Dockerfile
FROM ruby:2.7
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
COPY . /myapp
This Dockerfile uses the official Ruby image, installs necessary dependencies, and sets up the working directory for the Rails application.
Creating a Docker Compose File
Docker Compose is a tool for defining and running multi-container Docker applications. Here is an example docker-compose.yml
file for a Rails application:
# docker-compose.yml
version: '3'
services:
db:
image: postgres
web:
build: .
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -b '0.0.0.0'"
volumes:
- ".:/myapp"
ports:
- "3000:3000"
depends_on:
- db
This docker-compose.yml
file defines two services: db
(PostgreSQL) and web
(Rails application). The web
service is built using the Dockerfile and runs the Rails server.
Building and Running Containers
To build and run the containers defined in the Docker Compose file, use the following commands:
# Build the containers
docker-compose build
# Run the containers
docker-compose up
The docker-compose up
command builds the Docker images and starts the containers. The Rails application will be accessible at http://localhost:3000
.
Running Database Migrations
After starting the containers, you need to run the database migrations to set up the database schema. Use the following command:
# Run database migrations
docker-compose run web rails db:migrate
Managing Dependencies
To add new dependencies to your Rails application, update the Gemfile
and Gemfile.lock
, and then rebuild the Docker image:
# Install new gems
bundle install
# Rebuild the Docker image
docker-compose build
Using Environment Variables
Environment variables are used to configure your application. You can define environment variables in a .env
file and reference them in your Docker Compose file:
# .env
DATABASE_PASSWORD=mysecretpassword
# docker-compose.yml
version: '3'
services:
db:
image: postgres
environment:
POSTGRES_PASSWORD: ${DATABASE_PASSWORD}
web:
build: .
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -b '0.0.0.0'"
volumes:
- ".:/myapp"
ports:
- "3000:3000"
depends_on:
- db
environment:
DATABASE_PASSWORD: ${DATABASE_PASSWORD}
This setup ensures that sensitive information is kept out of the source code and can be easily changed per environment.
Debugging with Docker
To debug your Rails application running in Docker, you can use the Rails console or open a shell inside the running container:
# Open a Rails console
docker-compose run web rails console
# Open a shell inside the running container
docker-compose exec web bash
Deploying with Docker
To deploy your Rails application with Docker, you can use platforms like AWS, Google Cloud, or Heroku that support Docker containers. Here is an example of deploying to AWS Elastic Beanstalk:
# Install the AWS Elastic Beanstalk CLI
brew install awsebcli
# Initialize your Elastic Beanstalk application
eb init -p docker my-docker-app
# Create an environment and deploy
eb create my-docker-env
eb deploy
Conclusion
Using Docker with a Rails application provides consistency across development, testing, and production environments. By following this guide, you can set up Docker for your Rails application, manage dependencies, use environment variables, debug, and deploy your application efficiently.