Episodes

Resources

Download Source Code

Summary

# Terminal
docker images
docker container
docker container ls
docker system prune
docker build .
docker image build -t appname:latest .
docker-compose up
docker-compose down
docker-compose run app rails db:create
docker-compose run app yarn install
dcu
dcd
dcr
rails db:migrate

# ~/.zshrc | ~/.bashrc
alias dcu="rm -f tmp/pid/server.pid && docker-compose up"
alias dcd="docker-compose down"
alias dcr="docker-compose run app"

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

# Dockerfile
ARG RUBY_VERSION
FROM ruby:$RUBY_VERSION
LABEL [email protected]

ARG NODE_MAJOR
ARG BUNDLE_VERSION

RUN apt-get update
RUN apt-get install build-essential -y

RUN curl -SL https://deb.nodesource.com/setup_$NODE_MAJOR.x | bash -
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list

RUN apt-get update
RUN apt-get install nodejs -y
RUN apt-get install yarn -y

COPY Gemfile Gemfile.lock ./
RUN gem install bundler -v $BUNDLE_VERSION
RUN bundle install -j $(nproc)

RUN mkdir -p /app
WORKDIR /app

# docker-compose.yml
version: '2.4'
services:
  base: &base
    build:
      args:
        RUBY_VERSION: '2.6.5'
        NODE_MAJOR: '13'
        BUNDLE_VERSION: '2.1.2'
      context: .
      dockerfile: ./Dockerfile

  backend: &backend
    <<: *base
    volumes:
      - .:/app:cached
      - rails_cache:/app/tmp/cache
      - bundle:/usr/local/bunde
      - node_modules:/app/node_modules
      - packs:/app/public/packs
    depends_on:
      - postgres
      # - redis

  app:
    <<: *backend
    command: bundle exec rails server -b 0.0.0.0
    ports:
      - '3000:3000'
    environment:
      - DATABASE_URL=postgres://postgres:[email protected]:5432
      - WEBPACKER_DEV_SERVER_HOST=webpacker

  # sidekiq:
  #   <<: *backend
  #   command: bundle exec sidekiq -C config/sidekiq.yml
  #   environment:
  #     - DATABASE_URL=postgres://postgres:[email protected]:5432
  
  # redis:
  #   image: redis:latest

  webpacker:
    <<: *backend
    command: ./bin/webpack-dev-server
    ports:
      - '3035:3035'
    environment:
      - WEBPACKER_DEV_SERVER_HOST=0.0.0.0
      
  postgres:
    image: postgres:12.1
    ports:
      - 5432
    volumes:
      - postgres:/var/lib/postgresql/data

volumes:
  rails_cache:
  bundle:
  node_modules:
  packs:
  postgres:  

# config/database.yml

default: &default
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  host: <%= ENV.fetch("DATABASE_URL") { 'localhost' } %>

# config/environments/development.rb
config.webpacker.check_yarn_integrity = false