設定檔

使用設定檔,您可以定義一組啟用的設定檔,以便針對各種用途和環境調整 Compose 應用程式模型。

服務 頂層元素支援 profiles 屬性來定義已命名設定檔的清單。沒有 profiles 屬性的服務一律會啟用。

當列出的 profiles 都沒有符合啟用的設定檔時,Compose 會忽略服務,除非該服務是明確由指令指定。在這種情況下,其設定檔會新增到啟用的設定檔集中。

注意

所有其他頂層元素不受 profiles 影響,並且一律啟用。

對其他服務的參考(透過 linksextends 或共用資源語法 service:xxx)不會自動啟用原本會被啟用設定檔忽略的元件。Compose 反而會傳回錯誤。

說明範例

services:
  web:
    image: web_image

  test_lib:
    image: test_lib_image
    profiles:
      - test

  coverage_lib:
    image: coverage_lib_image
    depends_on:
      - test_lib
    profiles:
      - test

  debug_lib:
    image: debug_lib_image
    depends_on:
      - test_lib
    profiles:
      - debug

在上述範例中

  • 如果 Compose 應用程式模型在沒有啟用任何設定檔的情況下進行剖析,則它只包含 web 服務。
  • 如果啟用 test 設定檔,則模型包含 test_libcoverage_lib 服務,以及一律啟用的 web 服務。
  • 如果啟用 debug 設定檔,則模型包含 webdebug_lib 服務,但不包含 test_libcoverage_lib,因此模型對於 debug_libdepends_on 約束無效。
  • 如果啟用 debugtest 設定檔,則模型包含所有服務:webtest_libcoverage_libdebug_lib
  • 如果 Compose 是以 test_lib 作為要執行的明確服務來執行,則 test_libtest 設定檔會啟用,即使未啟用 test 設定檔也一樣。
  • 如果 Compose 是以 coverage_lib 作為要執行的明確服務來執行,則服務 coverage_lib 和設定檔 test 會啟用,而 test_lib 會被 depends_on 約束引入。
  • 如果 Compose 是以 debug_lib 作為要執行的明確服務來執行,則模型對於 debug_libdepends_on 約束仍然無效,因為 debug_libtest_lib 沒有列出共同的 profiles
  • 如果 Compose 是以 debug_lib 作為要執行的明確服務來執行,並且啟用設定檔 test,則會自動啟用設定檔 debug,並將服務 test_lib 作為相依性引入,啟動 debug_libtest_lib 服務。

瞭解如何在 Docker Compose 中使用 profiles