Rails_Docker_Deployment
Rails_Docker_Deployment
Nginx
• A step-by-step guide to containerizing a Rails
app, setting up a reverse proxy, load
balancing, and using NFS storage.
Setup Overview
• 1. Set up VM1 and install Docker.
• 2. Containerize the Rails app and Database.
• 3. Configure Nginx as a reverse proxy and load
balancer.
• 4. Enable persistent storage.
• 5. Use Docker Compose for easy deployment.
• 6. Implement rate limiting and DNS.
• 7. Set up NFS on VM2 for storage.
Setting Up VM1
• 1. Install Ubuntu Server on VM1.
• 2. Install Docker and Docker Compose.
• 3. Add user to the Docker group.
• Commands:
• sudo apt update
• sudo apt install -y docker.io docker-compose
• sudo usermod -aG docker $USER
Containerizing the Rails Application
• Create a Dockerfile:
• FROM ruby:3.0
• WORKDIR /app
• COPY . .
• RUN bundle install
• CMD ["rails", "server", "-b", "0.0.0.0"]
• Build the image:
• docker build -t my-rails-app .
Configuring the Database
Container
• Use PostgreSQL as the database.
• Database service in Docker Compose:
• services:
• db:
• image: postgres
• environment:
• POSTGRES_USER: user
• POSTGRES_PASSWORD: password
• networks:
Connecting Rails to the Database
• Database configuration in Rails
(database.yml):
• default: &default
• adapter: postgresql
• host: db
• username: user
• password: password
Setting Up Nginx as a Reverse
Proxy
• Nginx Configuration (nginx.conf):
• server {
• listen 80;
• location / {
• proxy_pass http://rails_app;
• }
• }
Load Balancing with Nginx
• Configure multiple Rails containers:
• upstream rails_app {
• server app1:3000;
• server app2:3000;
• server app3:3000;
• }
Enabling Persistent Storage
• Use Docker Volumes for DB and Nginx:
• volumes:
• db_data:
• nginx_config:
Deploying with Docker Compose
• Full docker-compose.yml Example:
• version: '3.8'
• services:
• db:
• image: postgres
• volumes:
• - db_data:/var/lib/postgresql/data
• app:
• build: .
Implementing Rate Limiting in
Nginx
• Rate limit example in nginx.conf:
• limit_req_zone $binary_remote_addr
zone=mylimit:10m rate=5r/s;
• location / {
• limit_req zone=mylimit burst=10;
• }
Setting Up DNS Resolution
• Modify /etc/hosts or use dnsmasq.
• Example DNS entry:
• 192.168.1.100 myapp.local
Configuring NFS Storage on VM2
• Install NFS Server on VM2:
• sudo apt install -y nfs-kernel-server
• sudo mkdir -p /storage
• echo "/storage
192.168.1.101(rw,sync,no_root_squash)" |
sudo tee -a /etc/exports
• sudo exportfs -a
• Mount it on VM1:
• sudo mount 192.168.1.102:/storage
Conclusion
• Summary of key steps:
• - Dockerized Rails app.
• - Configured database and persistent storage.
• - Set up Nginx as a reverse proxy and load
balancer.
• - Implemented rate limiting and DNS.
• - Mounted external storage via NFS.