Episodes

Resources

Download Source Code

Summary

# Terminal
touch Dockerfile
touch docker-compose.yml
touch .dockerignore
bin/dev
rails db:system:change --to=postgresql
docker compose run --rm app bin/rails db:create
dcr bin/rails db:create
rm Procfile.dev

# .dockerignore
.git*
log/*
tmp/*
Dockerfile
README.md

# Dockerfile.dev
ARG RUBY_VERSION
FROM ruby:$RUBY_VERSION

RUN apk add --no-cache --update build-base \
  bash \
  git \
  postgresql-dev \
  nodejs \
  yarn \
  imagemagick \
  vips \
  tzdata \
  && rm -rf /var/cache/apk/*

RUN mkdir -p /app
WORKDIR /app

COPY Gemfile Gemfile.lock ./
COPY package.json yarn.lock ./

RUN gem update --system
RUN bundle install -j $(nproc)
RUN yarn install

# docker-compose.yml
version: '3.8'
x-base: &base
  build:
    context: .
    dockerfile: ./Dockerfile.dev
    args:
      RUBY_VERSION: '3.0.2-alpine'
  stdin_open: true
  tty: true
  volumes:
    - .:/app:cached
    - bundle:/usr/local/bundle
    - node_modules:/app/node_modules
  environment:
    - DATABASE_URL=postgres://postgres:[email protected]:5432
  depends_on:
    - redis
    - postgres

services:
  app:
    <<: *base
    command: bin/rails server -p 3000 -b 0.0.0.0
    ports:
      - '3000:3000'

  css:
    <<: *base
    command: yarn build:css --watch

  js:
    <<: *base
    command: yarn build --watch

  # sidekiq:
  #   <<: *base
  #   command: bundle exec sidekiq -C config/sidekiq.yml

  redis:
    image: redis:latest
    restart: always

  postgres:
    image: postgres:14-alpine
    volumes:
      - postgres:/var/lib/postgresql/data
    environment:
      - POSTGRES_PASSWORD=postgres
    ports:
      - 5432

volumes:
  bundle:
  node_modules:
  postgres:

# bin/dev
#!/usr/bin/env bash

docker compose up

# .zshrc or .bashrc
alias dcd="docker compose down --remove-orphans"
alias dcr="docker compose run --rm app"