DockerでRails6の環境構築

# 前置き
DockerやGitなどはインストール、初期設定済みのものとします
新規にアプリを作成するときに毎度調べるのも面倒なので備忘録として

### 環境
- MacOS Monterey(cpu intel)
- Docker -v 20.10.16
- docker-compose v2.3.3
### 構築内容
- Rails6
- DB postgresql

# 手順
- 必要なディレクトリとフォルダを用意
Docments下に作成するアプリのディレクトリを作り、移動
``` 
$ mkdir myapp
$ cd myapp
```
必要なフォルダを作成
```
$ touch Dockerfile Gemfile Gemfile.lock docker-compose.yml entrypoint.sh
```
dockerfileに記述
```
FROM ruby:2.7.4

## nodejsとyarnはwebpackをインストールする際に必要
# yarnパッケージ管理ツールをインストール
RUN curl -sS 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

RUN apt-get update -qq && apt-get install -y nodejs build-essential libpq-dev postgresql-client yarn
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install

RUN yarn install --check-files
RUN bundle exec rails webpacker: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"]
```
gemfile
```
source 'https://rubygems.org'
gem 'rails', '6.1.4'
```
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 "$@"
```
docker-compose.yml
```
version: "3.9"
services:
  db:
    image: postgres
    volumes:
      - ./tmp/db:/var/lib/postgresql/data
    environment:
      POSTGRES_PASSWORD: password
  web:
    build: .
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    volumes:
      - .:/myapp
    ports:
      - "3000:3000"
    depends_on:
      - db
```
ターミナル
```
docker-compose run --no-deps web rails new . --force --database=postgresql
docker-compose run web bundle install
docker-compose build
```
build出来ず
bundlerとrubyのバージョンを指定し直す
```
docker-compose run web gem install bundler -v 2.1.2
```
gemfile.lock
```
BUNDLED WITH
 2.1.2
```
Gemfile
```
ruby '2.7.4'
```
```
docker-compose build
```
config/database.yml
```
default: &default
  adapter: postgresql
  encoding: unicode
  host: db
  username: postgres
  password: password
  pool: 5

development:
  <<: *default
  database: myapp_development


test:
  <<: *default
  database: myapp_test
```
ターミナル
```
docker-compose up -d
docker-compose run web rake db:create
```
webがexitしてしまう
```
=> Booting Puma
=> Rails 6.1.6 application starting in development 
=> Run `bin/rails server --help` for more startup options
Exiting
/usr/local/bundle/gems/webpacker-5.4.3/lib/webpacker/configuration.rb:103:in `rescue in load': Webpacker configuration file not found /affiliate_app/config/webpacker.yml. Please run rails webpacker:install Error: No such file or directory @ rb_check_realpath_internal - /affiliate_app/config/webpacker.yml (RuntimeError)
```
webpackerがインストールされて無いようす
```
$ docker-compose run web bundle exec rails webpacker:install
$ docker-compose up
```
localhost:3000で起動を確認

# Git
githubにてリポジトリを作成
```
git init
git add -A
git commit -m "first commit"
git branch -M main
git remote add origin git@github.com:名前/リポジトリ名
git push -u origin main
```
init後に、commitに含めたくないものは、init後にgitignoreに記述。
以降、コーディング後には
```
git add -A
git commit -m "message"
git push
```
で良い。ブランチとかはまた別で。


# 参考
https://zenn.dev/cherenkov/articles/7c5f70875c5cae