使用容器進行 Ruby on Rails 開發

先決條件

完成 容器化 Ruby on Rails 應用程式.

概述

在本節中,您將學習如何為您的容器化應用程式設定開發環境。這包括

  • 新增本地資料庫並持久化資料
  • 配置 Compose 以在您編輯和儲存程式碼時自動更新執行的 Compose 服務

新增本地資料庫並持久化資料

您可以使用容器來設定本地服務,例如資料庫。在本節中,您將更新 `compose.yaml` 檔案以定義資料庫服務和卷以持久化資料。

在克隆的倉庫目錄中,在 IDE 或文字編輯器中開啟 `compose.yaml` 檔案。您需要將資料庫密碼檔案作為環境變數新增到伺服器服務中,並指定要使用的金鑰檔案。

以下是更新後的 `compose.yaml` 檔案。

services:
  web:
    build: .
    command: bundle exec rails s -b '0.0.0.0'
    ports:
      - "3000:3000"
    depends_on:
      - db
    environment:
      - RAILS_ENV=test
    env_file: "webapp.env"
  db:
    image: postgres:latest
    secrets:
      - db-password
    environment:
      - POSTGRES_PASSWORD_FILE=/run/secrets/db-password
    volumes:
      - postgres_data:/var/lib/postgresql/data

volumes:
  postgres_data:
secrets:
  db-password:
    file: db/password.txt

注意

要了解有關 Compose 檔案中指令的更多資訊,請參見 Compose 檔案參考.

在您使用 Compose 執行應用程式之前,請注意此 Compose 檔案指定了一個 `password.txt` 檔案來儲存資料庫的密碼。您必須建立此檔案,因為它不包含在源倉庫中。

在克隆的倉庫目錄中,建立一個名為 `db` 的新目錄,並在該目錄中建立一個名為 `password.txt` 的檔案,其中包含資料庫的密碼。使用您喜歡的 IDE 或文字編輯器,將以下內容新增到 `password.txt` 檔案中。

mysecretpassword

儲存並關閉 `password.txt` 檔案。此外,在 `webapp.env` 檔案中,您可以更改連線到資料庫的密碼。

您現在應該在 `docker-ruby-on-rails` 目錄中具有以下內容。

.
├── Dockerfile
├── Gemfile
├── Gemfile.lock
├── README.md
├── Rakefile
├── app/
├── bin/
├── compose.yaml
├── config/
├── config.ru
├── db/
│   ├── development.sqlite3
│   ├── migrate
│   ├── password.txt
│   ├── schema.rb
│   └── seeds.rb
├── lib/
├── log/
├── public/
├── storage/
├── test/
├── tmp/
└── vendor

現在,執行以下 `docker compose up` 命令以啟動您的應用程式。

$ docker compose up --build

在 Ruby on Rails 中,`db:migrate` 是一個 Rake 任務,用於在資料庫上執行遷移。遷移是在一致且簡便的方式下隨時間推移更改資料庫模式結構的方法。

$ docker exec -it docker-ruby-on-rails-web-1 rake db:migrate RAILS_ENV=test

您將看到類似以下的提示

console == 20240710193146 CreateWhales: migrating ===================================== -- create_table(:whales) -> 0.0126s == 20240710193146 CreateWhales: migrated (0.0127s) ============================

重新整理您瀏覽器中的 https://:3000 並新增鯨魚。

在終端中按 `ctrl+c` 停止您的應用程式並再次執行 `docker compose up`,鯨魚將被持久化。

自動更新服務

使用 Compose Watch 在您編輯和儲存程式碼時自動更新執行的 Compose 服務。有關 Compose Watch 的更多詳細資訊,請參見 使用 Compose Watch.

在 IDE 或文字編輯器中開啟您的 `compose.yaml` 檔案,然後新增 Compose Watch 指令。以下是更新後的 `compose.yaml` 檔案。

services:
  web:
    build: .
    command: bundle exec rails s -b '0.0.0.0'
    ports:
      - "3000:3000"
    depends_on:
      - db
    environment:
      - RAILS_ENV=test
    env_file: "webapp.env"

    develop:
      watch:
        - action: rebuild
          path: .
  db:
    image: postgres:latest
    secrets:
      - db-password
    environment:
      - POSTGRES_PASSWORD_FILE=/run/secrets/db-password
    volumes:
      - postgres_data:/var/lib/postgresql/data

volumes:
  postgres_data:
secrets:
  db-password:
    file: db/password.txt

執行以下命令以使用 Compose Watch 執行您的應用程式。

$ docker compose watch

對本地計算機上應用程式原始檔的任何更改現在都將立即反映在執行的容器中。

在 IDE 或文字編輯器中開啟 `docker-ruby-on-rails/app/views/whales/index.html.erb` 並更新 `Whales` 字串,新增感嘆號。

-    <h1>Whales</h1>
+    <h1>Whales!</h1>

儲存對 `index.html.erb` 的更改,然後等待幾秒鐘以重建應用程式。再次轉到應用程式並驗證更新後的文字是否顯示。

在終端中按 `ctrl+c` 停止您的應用程式。

總結

在本節中,您瞭解瞭如何設定 Compose 檔案以新增本地資料庫並持久化資料。您還了解了如何使用 Compose Watch 在更新程式碼時自動重建和執行容器。

相關資訊

下一步

在下一節中,您將瞭解如何使用 GitHub Actions 設定 CI/CD 管道。