Skip
Arish's avatar

25. Rails with Docker Compose


Complete Rails Setup

yaml
1version: '3.8'
2
3services:
4  web:
5    build: .
6    command: bash -c "rm -f tmp/pids/server.pid && rails server -b 0.0.0.0"
7    volumes:
8      - .:/app
9      - bundle-cache:/usr/local/bundle
10      - node-modules:/app/node_modules
11    ports:
12      - "3000:3000"
13    depends_on:
14      db:
15        condition: service_healthy
16      redis:
17        condition: service_started
18    environment:
19      - DATABASE_URL=postgres://postgres:password@db/myapp_development
20      - REDIS_URL=redis://redis:6379
21      - RAILS_ENV=development
22
23  db:
24    image: postgres:15
25    volumes:
26      - postgres-data:/var/lib/postgresql/data
27    environment:
28      - POSTGRES_PASSWORD=password
29    healthcheck:
30      test: ["CMD-SHELL", "pg_isready -U postgres"]
31      interval: 5s
32      timeout: 5s
33      retries: 5
34
35  redis:
36    image: redis:7
37    volumes:
38      - redis-data:/data
39
40  sidekiq:
41    build: .
42    command: bundle exec sidekiq
43    volumes:
44      - .:/app
45      - bundle-cache:/usr/local/bundle
46    depends_on:
47      - db
48      - redis
49    environment:
50      - DATABASE_URL=postgres://postgres:password@db/myapp_development
51      - REDIS_URL=redis://redis:6379
52
53volumes:
54  postgres-data:
55  redis-data:
56  bundle-cache:
57  node-modules:

Usage

bash
1# Setup
2docker compose run web rails db:create db:migrate
3
4# Start
5docker compose up
6
7# Console
8docker compose run web rails console