Skip to content

Instantly share code, notes, and snippets.

@super-dog-human
Last active December 12, 2022 10:35
Show Gist options
  • Save super-dog-human/e742a1288d6593049824f3de29b3330f to your computer and use it in GitHub Desktop.
Save super-dog-human/e742a1288d6593049824f3de29b3330f to your computer and use it in GitHub Desktop.

Environments

  • M1 mac
  • Ruby 2.7.4
  • Rails 6(latest)
  • MySQL 5.7

Files

Dockerfile

# syntax=docker/dockerfile:1
FROM ruby:2.7.4
RUN wget --quiet -O - /tmp/pubkey.gpg https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \
  echo 'deb http://dl.yarnpkg.com/debian/ stable main' > /etc/apt/sources.list.d/yarn.list
RUN apt-get update -qq && apt-get install -y nodejs yarn
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install

# Add a script to be executed every time the container starts.
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000

# Configure the main process to run when running the image
CMD ["rails", "server", "-b", "0.0.0.0"]

docker-compose.yml

version: '3.9'
services:
  db:
    platform: linux/x86_64
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: 'password'
    volumes:
      - mysql:/var/lib/mysql
    ports:
      - "3306:3306"
    command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
  redis:
    image: redis:6
    ports:
      - "6379:6379"
    volumes:
      - redis:/var/lib/redis/data
  web:
    build: .
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    volumes:
      - .:/myapp
      - bundle:/usr/local/bundle
    ports:
      - "3000:3000"
    depends_on:
      - db
    environment:
      REDIS_URL: redis://redis:6379
    tty: true
    stdin_open: true
  sidekiq:
    build: .
    command: bundle exec sidekiq
    volumes:
      - .:/myapp
      - bundle:/usr/local/bundle
    depends_on:
      - db
      - redis
    environment:
      REDIS_URL: redis://redis:6379
volumes:
  bundle:
  mysql:
  redis:

entrypoint.sh

#!/bin/bash
set -e

# Remove a potentially pre-existing server.pid for Rails.
rm -f /myapp/tmp/pids/server.pid

# Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "$@"

Ruby

source 'https://rubygems.org'
gem 'rails', '~>6'

Gemfile.lock(blank file)

Launch

docker-compose run --no-deps web rails new . --force --database=mysql
vim config/database.yml

config/database.yml

default: &default
  adapter: mysql2
  encoding: utf8mb4
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: root
  password: password
  host: db

development:
  <<: *default
  database: myapp_development

test:
  <<: *default
  database: myapp_test

production:
  <<: *default
  database: myapp_production
  username: myapp
  password: <%= ENV['MYAPP_DATABASE_PASSWORD'] %>
docker-compose up --build
docker-compose exec web rails db:create

Commands

  • view logs
docker-compose logs web -f
  • execute rails command
docker-compose exec web bundle exec rails g scaffold page name:string title:string
  • bundle install
docker-compose exec web bundle install
  • enter shell
docker-compose exec web /bin/bash
  • await brakepoint
docker attach rails-app-web-1
  • remove unused image/volume
docker image prune
docker volume prune

TODO

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment