Creating a Ruby on Rails Development Environment with Docker
Docker is a helpful tool for making software development easier. In this guide, we’ll show you how to set up a development environment for Ruby on Rails using Docker. We’ll also make sure your data stays secure, even if you stop or remove containers. We’ll be working with PostgreSQL and Redis databases, and we’ll assume you’re getting a project from GitHub.
Installing Docker
Start by installing Docker on your computer. Docker Desktop is easy to use and works on both Windows and macOS. Get it from the official Docker website. I personally recommend OrbStack over Docker Desktop. (MacOS only yet)
Docker Compose
Docker Compose is a handy tool for managing applications with multiple parts. It lets you describe everything in one file. Here’s a simpler docker-compose.yml
for setting up Ruby on Rails with PostgreSQL and Redis, and making sure your data stays safe:
version: '3'
services:
web:
image: ruby:latest
ports:
- "3000:3000"
volumes:
- ./app:/app
depends_on:
- db
- redis
db:
image: postgres:latest
environment:
POSTGRES_DB: myapp_development
POSTGRES_USER: myapp_user
POSTGRES_PASSWORD: myapp_password
volumes:
- pg_data:/var/lib/postgresql/data
redis:
image: redis:latest
volumes:
- redis_data:/data
volumes:
pg_data:
redis_data:
In this simpler setup, we’ve introduced volumes (pg_data
for PostgreSQL and redis_data
for Redis) to make sure your data stays safe.
git clone https://github.com/yourusername/your-rails-project.git
Next, navigate to your project directory:
cd your-rails-project
Creating a Ruby on Rails Application:
Now, let’s consider you are pulling an existing Ruby on Rails project from GitHub. Clone the project into your desired directory.
Configuring PostgreSQL and Redis
In your Rails project, you’ll need to configure your database.yml
and any other environment-specific configuration files to work with the Docker containers for PostgreSQL and Redis. Ensure you have the correct host, username, password, and database name settings.
Dockerizing Your Ruby on Rails Application:
For your Rails application to work seamlessly with Docker, you’ll need to create a Dockerfile
in the root of your project. This file defines how your Ruby application should be built within a Docker container.
Here’s a basic Dockerfile
for a Ruby on Rails project:
# Use an official Ruby runtime as a parent image
FROM ruby:latest
# Set the working directory in the container
WORKDIR /app
# Copy the Gemfile and Gemfile.lock into the container
COPY Gemfile Gemfile.lock ./
# Install any needed gems mentioned in your Gemfile
RUN bundle install
# Copy the rest of your application code into the container
COPY . .
# Expose port 3000 to the outside world
EXPOSE 3000
# Start your Rails application
CMD ["rails", "server", "-b", "0.0.0.0"]
Starting the Environment
With your environment set up, it’s time to start it:
docker-compose up
Docker will keep your PostgreSQL and Redis data safe outside the containers, even if you stop or remove them.
Developing Your Application
You can start working on your Ruby on Rails project as usual. Docker will handle data safety for your PostgreSQL and Redis databases.
Shutting Down the Environment
When you’re done, you can shut down the environment:
docker-compose down
Conclusion
By using Docker volumes in your development environment, you’ve made sure that your PostgreSQL and Redis data stays secure, even if you stop or remove containers. Docker volumes are an important part of working with containers, making sure your data is always available and protected.