Community for developers to learn, share their programming knowledge. Register!
Deploying Ruby on Rails Applications

Using Docker for Ruby on Rails Application Deployment


In today's fast-paced development environment, deploying applications efficiently is crucial. This article will provide you with the necessary training on using Docker for Ruby on Rails application deployment. By leveraging Docker, you can create a consistent development and production environment, streamline your workflow, and simplify the deployment process. Let's dive into the details!

Creating a Dockerfile for Rails

The first step in deploying a Ruby on Rails application with Docker is to create a Dockerfile. This file contains the instructions that Docker uses to build your application's image. Below is a sample Dockerfile for a typical Rails application:

# Use the official Ruby image
FROM ruby:3.1

# Set the working directory
WORKDIR /app

# Install system dependencies
RUN apt-get update -qq && apt-get install -y nodejs postgresql-client

# Add Gemfile and Gemfile.lock
COPY Gemfile* ./

# Install Gems
RUN bundle install

# Add the rest of the application code
COPY . .

# Precompile assets
RUN RAILS_ENV=production bundle exec rake assets:precompile

# Start the application
CMD ["rails", "server", "-b", "0.0.0.0"]

Explanation of the Dockerfile

  • Base Image: The FROM ruby:3.1 line specifies the base image as Ruby 3.1.
  • Working Directory: The WORKDIR /app command sets the working directory inside the container.
  • System Dependencies: The RUN command installs necessary system packages like nodejs and postgresql-client, which are essential for Rails applications.
  • Dependencies: The COPY command brings in the Gemfile and Gemfile.lock, while RUN bundle install installs the gems.
  • Application Code: The application code is then copied into the container.
  • Asset Precompilation: Running rake assets:precompile prepares the assets for production use.
  • Start Command: Finally, the CMD command starts the Rails server.

Building the Docker Image

To build the Docker image, navigate to the directory containing your Dockerfile and run:

docker build -t my-rails-app .

Replace my-rails-app with an appropriate name for your application.

Managing Docker Containers

Once you have your Docker image ready, the next step is to manage your Docker containers. Containers are instances of your Docker images that can run your application. Hereā€™s how to manage them effectively.

Running a Container

To run a container from your image, use the following command:

docker run -d -p 3000:3000 --name my-running-app my-rails-app
  • The -d flag runs the container in detached mode.
  • The -p 3000:3000 flag maps port 3000 of the host to port 3000 of the container.
  • The --name flag assigns a name to your container for easier management.

Monitoring and Managing Containers

To list all running containers, use:

docker ps

You can stop a running container with:

docker stop my-running-app

And to remove it:

docker rm my-running-app

To view logs from your container, use:

docker logs my-running-app

Using Docker Compose

For more complex applications, you may want to use Docker Compose, which allows you to define and run multi-container Docker applications. Hereā€™s a basic docker-compose.yml for a Rails application:

version: '3.8'
services:
  web:
    build: .
    command: rails server -b 0.0.0.0
    volumes:
      - .:/app
    ports:
      - "3000:3000"
    depends_on:
      - db

  db:
    image: postgres:latest
    environment:
      POSTGRES_DB: mydatabase
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password

With this docker-compose.yml, you can start your entire application stack with a single command:

docker-compose up

Best Practices for Docker Deployment

When deploying Ruby on Rails applications with Docker, following best practices can help ensure a smooth and efficient deployment process.

1. Keep Images Lightweight

Always start with a minimal base image and only install necessary dependencies. This will reduce the size of your images and improve build times.

2. Use Multi-Stage Builds

Multi-stage builds allow you to separate the build environment from the production environment. This means you can compile assets in one stage and copy only the necessary files to the final image, resulting in a smaller and more efficient image.

# Build stage
FROM ruby:3.1 AS builder
WORKDIR /app
COPY Gemfile* ./
RUN bundle install
COPY . .
RUN RAILS_ENV=production bundle exec rake assets:precompile

# Production stage
FROM ruby:3.1
WORKDIR /app
COPY --from=builder /app .
CMD ["rails", "server", "-b", "0.0.0.0"]

3. Manage Environment Variables

Use environment variables to manage configuration settings, such as database credentials or API keys. You can define these in your docker-compose.yml file or pass them at runtime using the -e flag.

4. Handle Database Migrations

Ensure your database is ready for your application by running migrations after your containers are up. You can do this by executing:

docker-compose run web rake db:migrate

5. Use Health Checks

Implement health checks in your docker-compose.yml to monitor the health of your services. This allows you to ensure that your application is running smoothly.

services:
  web:
    ...
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3000"]
      interval: 1m30s
      timeout: 10s
      retries: 3

Summary

Using Docker for Ruby on Rails application deployment streamlines the process and provides a consistent environment across different stages of development. By creating a well-structured Dockerfile, effectively managing containers, and adhering to best practices, you can enhance the efficiency of your deployment strategy.

Through this article, we have explored the essential steps involved in deploying a Ruby on Rails application with Docker. From crafting a Dockerfile to managing containers and implementing best practices, you should now be well-equipped to tackle your deployment challenges. Embrace the power of Docker and transform your Rails application deployment experience!

Last Update: 31 Dec, 2024

Topics:
Ruby on Rails