0
0
Ruby on Railsframework~5 mins

Docker deployment in Ruby on Rails

Choose your learning style9 modes available
Introduction

Docker deployment helps you package your Rails app with everything it needs to run. This makes it easy to move and run your app anywhere without setup problems.

You want to share your Rails app with others who may have different computers.
You need to run your app on a server or cloud without installing Ruby or dependencies manually.
You want to keep your app environment consistent between development and production.
You want to test your app in a clean environment without affecting your computer.
You want to automate deployment steps for faster updates.
Syntax
Ruby on Rails
FROM ruby:3.2
WORKDIR /app
COPY Gemfile* ./
RUN bundle install
COPY . .
CMD ["rails", "server", "-b", "0.0.0.0"]

FROM sets the base image with Ruby installed.

WORKDIR sets the folder inside the container where commands run.

Examples
Basic Dockerfile to run a Rails server inside a container.
Ruby on Rails
FROM ruby:3.2
WORKDIR /app
COPY Gemfile* ./
RUN bundle install
COPY . .
CMD ["rails", "server", "-b", "0.0.0.0"]
Command to build the Docker image named myrailsapp from the current folder.
Ruby on Rails
docker build -t myrailsapp .
Run the container and map port 3000 inside the container to port 3000 on your computer.
Ruby on Rails
docker run -p 3000:3000 myrailsapp
Sample Program

This Dockerfile sets up a Rails app to run inside a container. It installs Ruby gems, copies your app code, exposes port 3000, and starts the Rails server so you can access it from your browser.

Ruby on Rails
FROM ruby:3.2

# Set working directory
WORKDIR /app

# Copy Gemfile and Gemfile.lock
COPY Gemfile Gemfile.lock ./

# Install gems
RUN bundle install

# Copy all app files
COPY . .

# Expose port 3000
EXPOSE 3000

# Start Rails server binding to all interfaces
CMD ["rails", "server", "-b", "0.0.0.0"]
OutputSuccess
Important Notes

Make sure your Gemfile.lock is present to speed up gem installation.

Use EXPOSE 3000 to document the port your app uses.

Binding Rails server to 0.0.0.0 allows access from outside the container.

Summary

Docker deployment packages your Rails app and its environment together.

Use a Dockerfile to define how your app runs inside a container.

Build and run your Docker image to test and deploy your app easily anywhere.