Rails #
Ruby on Rails adalah framework web yang sangat matang, opinionated, dan produktif. Namun ironisnya, setup local development Rails sering menjadi sumber masalah di tim:
- Versi Ruby berbeda-beda
- Native dependency (openssl, libpq, node, yarn) sering bentrok
- Setup database manual
- Onboarding developer baru memakan waktu lama
Karena itu, Dockerfile dan Docker Compose hampir menjadi kebutuhan wajib untuk Rails modern, terutama di lingkungan tim.
Artikel ini membahas secara mendetail dan rinci:
- Dockerfile Ruby on Rails khusus local development
- Docker Compose sebagai orkestrator environment lokal
- Workflow development Rails yang efisien
- Best practice dan anti-pattern yang sering terjadi
Fokus utama artikel ini adalah Docker Compose untuk local development, bukan production.
Filosofi Docker Rails #
Tujuan utama Docker di local development Rails:
- Menyamakan versi Ruby, OS, dan native dependency
- Menghilangkan masalah works on my machine
- Mempercepat onboarding developer baru
- Mendukung reload otomatis (Rails code reloading)
Bukan tujuan utama:
- Image sekecil mungkin
- Hardening security ekstrem
- Setup production-grade
Local development fokus pada konsistensi environment dan kecepatan iterasi, bukan optimasi runtime.
Gambaran Arsitektur #
Arsitektur umum Rails lokal:
Developer
|
v
Docker Compose
|
+--> rails-app (puma / rails server)
|
+--> database (PostgreSQL)
|
+--> redis (opsional)
Struktur Project Ruby on Rails #
Struktur project Rails standar:
.
├── app/
├── bin/
├── config/
├── db/
├── Gemfile
├── Gemfile.lock
├── Dockerfile
├── docker-compose.yml
├── .env
└── .dockerignore
Karakteristik Dockerfile Rails #
| Aspek | Local Development | Production |
|---|---|---|
| Server | rails server / puma | puma |
| Reload | Aktif | Tidak |
| Debug | Aktif | Dimatikan |
| Image size | Tidak kritis | Penting |
| Asset | Compile on the fly | Precompile |
Kesalahan umum: menggunakan Dockerfile production untuk local development.
Dockerfile Ruby on Rails #
Prinsip Dasar #
- Gunakan Ruby official image
- Install Node.js & Yarn
- Cache Gemfile
- Jalankan Rails development server
Contoh Dockerfile #
FROM ruby:3.3-slim
WORKDIR /app
# Install OS dependencies
RUN apt-get update -qq && apt-get install -y \
build-essential \
libpq-dev \
nodejs \
yarn \
git \
curl \
&& rm -rf /var/lib/apt/lists/*
# Install bundler
RUN gem install bundler
# Cache gems
COPY Gemfile Gemfile.lock ./
RUN bundle install
# Copy source code
COPY . .
EXPOSE 3000
CMD ["bin/rails", "server", "-b", "0.0.0.0"]
Penjelasan Dockerfile #
1. Base Image #
FROM ruby:3.3-slim
- Versi Ruby eksplisit
- Image ringan
- Cocok untuk local dev
2. Native Dependency #
Rails membutuhkan:
- Compiler (
build-essential) - Database client (
libpq-dev) - Node.js & Yarn untuk asset
3. Cache Gemfile #
COPY Gemfile Gemfile.lock ./
RUN bundle install
Layer ini jarang berubah → rebuild lebih cepat.
4. Rails Server #
CMD ["bin/rails", "server", "-b", "0.0.0.0"]
Rails development server mendukung reload otomatis.
Docker Compose #
Peran Docker Compose #
Docker Compose menyatukan:
- Rails app
- Database
- Redis (opsional)
Dalam satu perintah.
Contoh docker-compose.yml #
version: "3.9"
services:
web:
container_name: rails-app
build:
context: .
dockerfile: Dockerfile
ports:
- "3000:3000"
volumes:
- .:/app
env_file:
- .env
depends_on:
- db
- redis
db:
image: postgres:16-alpine
container_name: postgres-db
environment:
POSTGRES_DB: app_development
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
ports:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data
redis:
image: redis:7-alpine
container_name: redis
ports:
- "6379:6379"
volumes:
postgres_data:
Penjelasan docker-compose.yml #
1. Service Rails App #
Volume Mount #
volumes:
- .:/app
- Sinkronisasi source code
- Reload Rails bekerja optimal
Port Mapping #
ports:
- "3000:3000"
Akses aplikasi via http://localhost:3000.
Service Database #
- PostgreSQL image resmi
- Data persisten via volume
Service Redis (Opsional) #
Digunakan untuk:
- Cache
- ActiveJob
- ActionCable
Workflow Development #
Jalankan stack:
docker compose up --buildSetup database:
docker compose exec web bin/rails db:setupEdit code
Rails reload otomatis
Anti-Pattern #
- Satu Dockerfile untuk local & production
- Tidak mount volume
- Menyimpan secret di image
- Rails terasa berat karena setup salah
Best Practice #
1. Gunakan .dockerignore
#
.git
log/
tmp/
.env
node_modules
2. Simpan Config di ENV #
Gunakan config/database.yml berbasis ENV.
3. Jangan Precompile Asset di Local #
Asset compile hanya untuk production.
Penutup #
Dockerfile dan Docker Compose untuk local development Ruby on Rails seharusnya menekankan konsistensi environment, kemudahan setup, dan kecepatan iterasi.
Dengan setup yang tepat:
- Onboarding developer jauh lebih cepat
- Environment lokal seragam
- Rails kembali terasa menyenangkan