使用容器進行 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: 正在遷移 ===================================== -- create_table(:whales) -> 0.0126s == 20240710193146 CreateWhales: 已遷移 (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 在更新程式碼時自動重建並執行容器。

相關資訊

後續步驟

在下一節中,你將學習如何在部署到 Kubernetes 之前在本地測試和除錯你的工作負載。