【環境構築】Rails + Docker + Bootstrap

# 開発をするためのディレクトリを作成する
```
mkdir 自分で開発するアプリの名前
```
# Dockerfileを作成する
```
touch Dockerfile
```
# Dockerfileの中身を書く
```
# 使いたいバージョンを決めて{{}}をruby:tag名の形で置き換えてください
# 例: ARG RUBY_VERSION=ruby:3.2.2
ARG RUBY_VERSION={{rubyのimage名:tag名}}
# {{}}を丸ごと使いたいnodeのversionに置き換えてください、小数点以下はいれないでください
# 例: ARG NODE_VERSION=19
ARG NODE_VERSION={{nodeのversion}}

FROM $RUBY_VERSION
ARG RUBY_VERSION
ARG NODE_VERSION
ENV LANG C.UTF-8
ENV TZ Asia/Tokyo
RUN curl -sL https://deb.nodesource.com/setup_${NODE_VERSION}.x | bash - \
&& wget --quiet -O - /tmp/pubkey.gpg https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - \
&& echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list \
&& apt-get update -qq \
&& apt-get install -y build-essential nodejs yarn
RUN mkdir /app
WORKDIR /app
RUN gem install bundler
COPY Gemfile /app/Gemfile
COPY Gemfile.lock /app/Gemfile.lock
COPY yarn.lock /app/yarn.lock
RUN bundle install
RUN yarn install
COPY . /app
```
# compose.ymlを作成する
```
touch compose.yml
```
# compose.ymlの中身を書く
```
services:
  db:
    image: postgres:latest
    platform: linux/amd64
    environment:
      TZ: Asia/Tokyo
      POSTGRES_PASSWORD: password
    volumes:
      - postgres_data:/var/lib/postgresql/data
    ports:
      - 5432:5432
  web:
    build: .
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    tty: true
    stdin_open: true
    volumes:
      - .:/app
      - bundle_data:/usr/local/bundle:cached
      - node_modules:/app/node_modules
    environment:
      TZ: Asia/Tokyo
    ports:
      - "3000:3000"
    depends_on:
      - db
volumes:
  postgres_data:
  bundle_data:
  node_modules:
  ```
  # Dockerfile内で指定しているファイルを作成する
  ```
  touch Gemfile Gemfile.lock yarn.lock
  ```
  # Gemfileに使用するRailsのバージョンを書く
  ```
  # frozen_string_literal: true

source "https://rubygems.org"

gem "rails", "~> 7.0.0"
```
# rails new
```
docker-compose run --rm --no-deps web rails new . --force --database=mysql --css=bootstrap
```
・オプションはご自由に。
```
Overwrite /app/Gemfile? (enter "h" for help) [Ynaqdhm]
```
と聞かれたらyを入力。
# config/database.ymlを編集する
```
# PostgreSQL. Versions 9.3 and up are supported.
#
# Install the pg driver:
#   gem install pg
# On macOS with Homebrew:
#   gem install pg -- --with-pg-config=/usr/local/bin/pg_config
# On macOS with MacPorts:
#   gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config
# On Windows:
#   gem install pg
#       Choose the win32 build.
#       Install PostgreSQL and put its /bin directory on your path.
#
# Configure Using Gemfile
# gem "pg"
#
default: &default
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see Rails configuration guide
  # https://guides.rubyonrails.org/configuring.html#database-pooling
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  host: db
  username: postgres
  password: password

development:
  <<: *default
  database: app_development

  # The specified database role being used to connect to postgres.
  # To create additional roles in postgres see `$ createuser --help`.
  # When left blank, postgres will use the default role. This is
  # the same name as the operating system user running Rails.
  #username: app

  # The password associated with the postgres role (username).
  #password:

  # Connect on a TCP socket. Omitted by default since the client uses a
  # domain socket that doesn't need configuration. Windows does not have
  # domain sockets, so uncomment these lines.
  #host: localhost

  # The TCP port the server listens on. Defaults to 5432.
  # If your server runs on a different port number, change accordingly.
  #port: 5432

  # Schema search path. The server defaults to $user,public
  #schema_search_path: myapp,sharedapp,public

  # Minimum log levels, in increasing order:
  #   debug5, debug4, debug3, debug2, debug1,
  #   log, notice, warning, error, fatal, and panic
  # Defaults to warning.
  #min_messages: notice

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *default
  database: app_test

# As with config/credentials.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
# ever seen by anyone, they now have access to your database.
#
# Instead, provide the password or a full connection URL as an environment
# variable when you boot the app. For example:
#
#   DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase"
#
# If the connection URL is provided in the special DATABASE_URL environment
# variable, Rails will automatically merge its configuration values on top of
# the values provided in this file. Alternatively, you can specify a connection
# URL environment variable explicitly:
#
#   production:
#     url: <%= ENV["MY_APP_DATABASE_URL"] %>
#
# Read https://guides.rubyonrails.org/configuring.html#configuring-a-database
# for a full overview on how database connection configuration can be specified.
#
production:
  <<: *default
  database: app_production
  username: app
  password: <%= ENV["APP_DATABASE_PASSWORD"] %>
  ```
  # Procfile.devを編集する(Option)
  ```
  web: unset PORT && env RUBY_DEBUG_OPEN=true bin/rails server
js: yarn build --watch
css: yarn build:css --watch
```
を下記のような形に変更するとデバッグツールが使いやすくなる
```
js: yarn build --watch
css: yarn build:css --watch
```
# コンテナの立ち上げ
```
docker compose build
```
```
docker compose up
```
# gem・JavaScriptパッケージをインストールする
docker compose up が実行されているターミナルとは別のターミナルで実行。
```
docker compose run web bundle install
```
```
docker compose run web yarn install
```
# データベースの初期化・マイグレーションファイル適応
docker compose up が実行されているターミナルとは別のターミナルで実行。
```
docker compose exec web rails db:drop
```
```
docker compose exec web rails db:create
```
```
docker compose exec web rails db:migrate
```
# CSS, JavaScript用のサーバー起動
docker compose up が実行されているターミナルとは別のターミナルで実行。
```
docker compose exec web bin/dev
```
# ブラウザにて画面が表示されるか確認する
http://localhost:3000/ にアクセス。Railsの初期画面が表示されたら成功。