使用容器進行 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://127.0.0.1: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 管道。