【環境構築】DockerでRails7を構築する

# 手順

## 必要なファイルを作成
```terminal
mkdir ~/Desktop/sample
```

```terminal
cd ~/Desktop/sample
```

```terminal
touch {Dockerfile,docker-compose.yml,Gemfile,Gemfile.lock,entrypoint.sh}
```



## 各ファイルを編集

```Gemfile:Gemfile
source 'https://rubygems.org'

gem 'rails', '~> 7.0.2'
```

```Dockerfile:Dockerfile
FROM ruby:3.1

RUN apt-get update -qq && apt-get install -y nodejs npm postgresql-client

RUN mkdir /sample-app
WORKDIR /sample-app

COPY Gemfile Gemfile.lock ./
RUN bundle install

COPY . .

COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]

EXPOSE 8080

CMD ["rails", "server", "-b", "0.0.0.0"]
```

```docker-compose.yml
services:
  db:
    image: postgres
    volumes:
      - ./tmp/db:/var/lib/postgresql/data
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: password
  web:
    build: .
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 8080 -b '0.0.0.0'"
    volumes:
      - .:/sample-app
    ports:
      - "8080:8080"
    depends_on:
      - db
```

```entrypoint.sh
#!/bin/bash
set -e

rm -f /sample-app/tmp/pids/server.pid

exec "$@"
```

## ビルド実行
```terminal
docker compose build
```

:::note warn
buildしていないと、後続で `rails new` をした際にエラーが発生する
```terminal
Could not find gem 'sprockets-rails' in locally installed gems.
Run `bundle install` to install missing gems.
       rails  turbo:install stimulus:install
Could not find gem 'sprockets-rails' in locally installed gems.
```
:::



## rails new
```terminal
docker-compose run web rails new . --force --no-deps --database=postgresql
```

```terminal:実行結果
Bundle complete! 14 Gemfile dependencies, 75 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.

...

Appending: pin_all_from "app/javascript/controllers", under: "controllers"
      append  config/importmap.rb
```

:::note warn
この段階で`config/database.yml`が作成されていない場合、docker-desktopアプリ内の古いContainersやImages、Volumesを削除してからやり直す
:::


## 既存の`config/database.yml`の記述内容を全て消し、下記に書き換える
```config/database.yml
default: &default
  adapter: postgresql
  encoding: unicode
  host: db
  username: postgres
  password: password
  pool: 5

development:
  <<: *default
  database: sample-app_development

test:
  <<: *default
  database: sample-app_test
```

## 再度ビルド実行
```terminal
docker compose build
```

## DB作成前に起動させておく
```terminal
docker-compose up -d
```

## DB作成
```terminal
docker-compose run web rails db:create
```

```terminal:実行結果
(base) test@TestnoMBP sample % docker-compose run web rails db:create
[+] Building 0.0s (0/0)                                                                                                                                                                docker:desktop-linux
[+] Creating 1/0
 ✔ Container sample-db-1  Created                                                                                                                                                            0.0s
[+] Running 1/1
 ✔ Container sample-db-1  Started                                                                                                                                                            0.1s
[+] Building 0.0s (0/0)                                                                                                                                                                docker:desktop-linux
Created database 'sample-app_development'
Created database 'sample-app_test'
```

## ブラウザにアクセス

:::note
http://localhost:8080/ にアクセスし、ウェルカム画面が表示されればOK！
:::

![スクリーンショット 2025-01-13 17.34.41.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2689074/3ae99819-eb68-ab99-9ab8-669b6fc66639.png)


# Dockerの基本コマンド

比較的良く使う基本的なコマンドを記載


### ビルド
```terminal
docker-compose build
```

### 起動
```terminal
docker-compose up
```
```terminal
docker-compose up -d
```

### 停止＆削除
```terminal
docker-compose down
```
起動時にデタッチしていなければ `command + c`


### 起動確認
```terminal
docker compose ps
```

### ログ確認
```terminal
docker-compose logs
```

### コンテナ内でコマンドを実行したい時
```terminal
docker-compose exec web /bin/bash
```

### お掃除
```terminal
docker system prune
```

```terminal
docker system prune -a
```

### Dockerコマンド確認
```terminal
docker --help
```