Skip
Arish's avatar

23. Defining Services


Service Configuration

yaml
1services:
2  web:
3    # Build from Dockerfile
4    build: .
5    # Or use image
6    image: nginx:latest
7    
8    # Container name
9    container_name: my-web
10    
11    # Port mapping
12    ports:
13      - "3000:3000"
14    
15    # Environment variables
16    environment:
17      - RAILS_ENV=development
18      - SECRET_KEY_BASE=abc123
19    
20    # Or from file
21    env_file:
22      - .env
23    
24    # Volume mounts
25    volumes:
26      - .:/app
27      - bundle-cache:/usr/local/bundle
28    
29    # Dependencies
30    depends_on:
31      - db
32      - redis
33    
34    # Restart policy
35    restart: unless-stopped

Build Options

yaml
1services:
2  web:
3    build:
4      context: .
5      dockerfile: Dockerfile.dev
6      args:
7        RUBY_VERSION: "3.2"

Health Checks

yaml
1services:
2  web:
3    healthcheck:
4      test: ["CMD", "curl", "-f", "http://localhost:3000/health"]
5      interval: 30s
6      timeout: 10s
7      retries: 3