|
| 1 | +# Spec-Driven Rails App Deployment (End-to-End Documentation) |
| 2 | + |
| 3 | +## Overview |
| 4 | +This document outlines the complete end-to-end process of building, containerizing, and deploying a Ruby on Rails 8 application using Docker and Kamal on an Amazon Lightsail instance. |
| 5 | + |
| 6 | +--- |
| 7 | + |
| 8 | +## 1. Project Setup |
| 9 | + |
| 10 | +### 1.1 Create Rails Application |
| 11 | + |
| 12 | +```bash |
| 13 | +rails new spec_driven_app --database=sqlite3 |
| 14 | +cd spec_driven_app |
| 15 | +``` |
| 16 | + |
| 17 | +### 1.2 Initial Configuration |
| 18 | + |
| 19 | +- Ruby version: 3.3.6 |
| 20 | +- Rails version: 8.x |
| 21 | +- Database: SQLite (for testing) |
| 22 | +- Environment: Production-ready Docker setup |
| 23 | + |
| 24 | +### 1.3 Install Dependencies |
| 25 | + |
| 26 | +```bash |
| 27 | +bundle install |
| 28 | +``` |
| 29 | + |
| 30 | +--- |
| 31 | + |
| 32 | +## 2. Version Control |
| 33 | + |
| 34 | +### 2.1 Initialize Git |
| 35 | + |
| 36 | +```bash |
| 37 | +git init |
| 38 | +git add . |
| 39 | +git commit -m "Initial Rails 8 app" |
| 40 | +``` |
| 41 | + |
| 42 | +### 2.2 Create GitHub Repository |
| 43 | + |
| 44 | +```bash |
| 45 | +git remote add origin https://github.com/apperph/github-actions-test.git |
| 46 | +git branch -M main |
| 47 | +git push -u origin main |
| 48 | +``` |
| 49 | + |
| 50 | +**Important:** |
| 51 | + |
| 52 | +Never commit `.kamal/secrets` or `.env` |
| 53 | + |
| 54 | +Add to `.gitignore`: |
| 55 | + |
| 56 | +```gitignore |
| 57 | +.kamal/secrets |
| 58 | +.env |
| 59 | +``` |
| 60 | + |
| 61 | +--- |
| 62 | + |
| 63 | +## 3. Deployment Setup (AWS Lightsail) |
| 64 | + |
| 65 | +### 3.1 Create Lightsail Instance |
| 66 | + |
| 67 | +- OS: Ubuntu |
| 68 | +- Region: Singapore (ap-southeast-1) |
| 69 | +- Instance Type: Upgraded (to avoid memory issues) |
| 70 | +- Static IP: 13.213.250.166 |
| 71 | + |
| 72 | +### 3.2 Configure Firewall |
| 73 | + |
| 74 | +| Port | Purpose | |
| 75 | +|------|---------| |
| 76 | +| 22 | SSH | |
| 77 | +| 80 | HTTP | |
| 78 | +| 443 | HTTPS | |
| 79 | + |
| 80 | +### 3.3 SSH Setup |
| 81 | + |
| 82 | +Download the `.pem` key and configure: |
| 83 | + |
| 84 | +```bash |
| 85 | +chmod 400 ~/Downloads/rails-test-key.pem |
| 86 | +``` |
| 87 | + |
| 88 | +Add to SSH config: |
| 89 | + |
| 90 | +```bash |
| 91 | +nano ~/.ssh/config |
| 92 | +``` |
| 93 | + |
| 94 | +```sshconfig |
| 95 | +Host rails-test-lightsail |
| 96 | + HostName 13.213.250.166 |
| 97 | + User ubuntu |
| 98 | + IdentityFile ~/Downloads/rails-test-key.pem |
| 99 | +``` |
| 100 | + |
| 101 | +Test connection: |
| 102 | + |
| 103 | +```bash |
| 104 | +ssh rails-test-lightsail |
| 105 | +``` |
| 106 | + |
| 107 | +--- |
| 108 | + |
| 109 | +## 4. Docker Setup |
| 110 | + |
| 111 | +Ensure Docker is running locally: |
| 112 | + |
| 113 | +```bash |
| 114 | +docker info |
| 115 | +``` |
| 116 | + |
| 117 | +If Docker is not running: |
| 118 | + |
| 119 | +```bash |
| 120 | +open -a Docker |
| 121 | +``` |
| 122 | + |
| 123 | +--- |
| 124 | + |
| 125 | +## 5. Deployment with Kamal |
| 126 | + |
| 127 | +### 5.1 Install Kamal |
| 128 | + |
| 129 | +```bash |
| 130 | +gem install kamal |
| 131 | +``` |
| 132 | + |
| 133 | +### 5.2 Initialize Kamal |
| 134 | + |
| 135 | +```bash |
| 136 | +kamal init |
| 137 | +``` |
| 138 | + |
| 139 | +### 5.3 Configure `config/deploy.yml` |
| 140 | + |
| 141 | +```yaml |
| 142 | +service: spec_driven_app |
| 143 | +image: ghcr.io/apperph/github-actions-test |
| 144 | + |
| 145 | +servers: |
| 146 | + web: |
| 147 | + - 13.213.250.166 |
| 148 | + |
| 149 | +registry: |
| 150 | + server: ghcr.io |
| 151 | + username: YOUR_USERNAME |
| 152 | + password: |
| 153 | + - KAMAL_REGISTRY_PASSWORD |
| 154 | + |
| 155 | +env: |
| 156 | + clear: |
| 157 | + RAILS_ENV: production |
| 158 | + PORT: 80 |
| 159 | +``` |
| 160 | +
|
| 161 | +### 5.4 Setup Server |
| 162 | +
|
| 163 | +```bash |
| 164 | +bin/kamal setup |
| 165 | +``` |
| 166 | + |
| 167 | +### 5.5 Deploy Application |
| 168 | + |
| 169 | +```bash |
| 170 | +bin/kamal deploy |
| 171 | +``` |
| 172 | + |
| 173 | +--- |
| 174 | + |
| 175 | +## 6. Debugging & Issues Encountered |
| 176 | + |
| 177 | +### Issue 1: SSH Authentication Failed |
| 178 | + |
| 179 | +**Error:** |
| 180 | +Authentication failed for user ubuntu |
| 181 | + |
| 182 | +**Fix:** |
| 183 | +- Ensure correct `.pem` path |
| 184 | +- Check permissions: |
| 185 | + ```bash |
| 186 | + chmod 400 key.pem |
| 187 | + ``` |
| 188 | + |
| 189 | +### Issue 2: Docker Not Running |
| 190 | + |
| 191 | +**Error:** |
| 192 | +Cannot connect to Docker daemon |
| 193 | + |
| 194 | +**Fix:** |
| 195 | +```bash |
| 196 | +open -a Docker |
| 197 | +``` |
| 198 | + |
| 199 | +### Issue 3: Lightsail Instance Timeout |
| 200 | + |
| 201 | +**Error:** |
| 202 | +Operation timed out |
| 203 | + |
| 204 | +**Fix:** |
| 205 | +- Start instance in AWS console |
| 206 | +- Verify port 22 is open |
| 207 | + |
| 208 | +### Issue 4: Deployment Lock Error |
| 209 | + |
| 210 | +**Error:** |
| 211 | +Deploy lock already in place |
| 212 | + |
| 213 | +**Fix:** |
| 214 | +```bash |
| 215 | +bin/kamal lock release |
| 216 | +``` |
| 217 | + |
| 218 | +### Issue 5: Container Unhealthy |
| 219 | + |
| 220 | +**Error:** |
| 221 | +target failed to become healthy |
| 222 | + |
| 223 | +**Cause:** Low memory (512 MB instance) |
| 224 | + |
| 225 | +**Fix:** |
| 226 | +- Upgrade Lightsail instance |
| 227 | +- Redeploy |
| 228 | + |
| 229 | +### Issue 6: GHCR Connection Timeout |
| 230 | + |
| 231 | +**Error:** |
| 232 | +context deadline exceeded |
| 233 | + |
| 234 | +**Fix:** |
| 235 | +```bash |
| 236 | +docker login ghcr.io |
| 237 | +``` |
| 238 | + |
| 239 | +### Issue 7: GitHub Secret Leak |
| 240 | + |
| 241 | +**Error:** |
| 242 | +Push blocked due to exposed token |
| 243 | + |
| 244 | +**Fix:** |
| 245 | +- Remove `.kamal/secrets` from Git |
| 246 | +- Rotate GitHub token |
| 247 | +- Amend commit |
| 248 | + |
| 249 | +--- |
| 250 | + |
| 251 | +## 7. Important Notes |
| 252 | + |
| 253 | +### Infrastructure |
| 254 | +- Public IP: 13.213.250.166 |
| 255 | +- SSH Host: rails-test-lightsail |
| 256 | +- Registry: ghcr.io/apperph/github-actions-test |
| 257 | + |
| 258 | +### Key Commands |
| 259 | + |
| 260 | +- **Deploy:** |
| 261 | + ```bash |
| 262 | + bin/kamal deploy |
| 263 | + ``` |
| 264 | + |
| 265 | +- **Logs:** |
| 266 | + ```bash |
| 267 | + bin/kamal app logs |
| 268 | + ``` |
| 269 | + |
| 270 | +- **SSH:** |
| 271 | + ```bash |
| 272 | + ssh rails-test-lightsail |
| 273 | + ``` |
| 274 | + |
| 275 | +- **Docker Logs:** |
| 276 | + ```bash |
| 277 | + docker logs <container_id> |
| 278 | + ``` |
| 279 | + |
| 280 | +### Common Pitfalls |
| 281 | +- Double `ghcr.io` in image path |
| 282 | +- Docker not running locally |
| 283 | +- Secrets committed to repo |
| 284 | +- Instance too small (memory) |
| 285 | + |
| 286 | +--- |
| 287 | + |
| 288 | +## 8. Best Practices / Lessons Learned |
| 289 | + |
| 290 | +### Infrastructure |
| 291 | +- Use at least 1GB+ RAM for Rails apps |
| 292 | +- Always attach a static IP |
| 293 | + |
| 294 | +### Security |
| 295 | +- Never commit: |
| 296 | + - `.env` |
| 297 | + - `.kamal/secrets` |
| 298 | +- Rotate tokens immediately if exposed |
| 299 | + |
| 300 | +### Deployment |
| 301 | +- Always run `docker login ghcr.io` on the server before deploy |
| 302 | +- Check container health: |
| 303 | + ```bash |
| 304 | + docker ps |
| 305 | + ``` |
| 306 | + |
| 307 | +### Debugging Tips |
| 308 | +- If deploy hangs → check logs immediately |
| 309 | +- If SSH fails → check instance + firewall |
| 310 | +- If container unhealthy → inspect logs |
| 311 | + |
| 312 | +### Workflow Recommendation |
| 313 | + |
| 314 | +Future improvement: |
| 315 | +PM edits business spec → GitHub commit → GitHub Actions builds image → Kamal deploys automatically |
| 316 | + |
| 317 | +--- |
| 318 | + |
| 319 | +## Final Status |
| 320 | + |
| 321 | +- ✅ Rails 8 App Running |
| 322 | +- ✅ Dockerized |
| 323 | +- ✅ Deployed via Kamal |
| 324 | +- ✅ Hosted on AWS Lightsail |
| 325 | +- ✅ Zero-downtime deployment working |
| 326 | + |
| 327 | +## Access the App |
| 328 | + |
| 329 | +**URL:** http://13.213.250.166 |
| 330 | +``` |
| 331 | +
|
| 332 | +This is the full file — no broken parts. |
| 333 | +Just copy from `# Spec-Driven Rails App Deployment...` to the end and save it. |
| 334 | +
|
| 335 | +If you're still having trouble copying, tell me your device (Windows/Mac/Android) and I'll give you the easiest method for your device. |
0 commit comments