トップレベル要素 configs

config は Docker イメージの再ビルドを必要とせずに、サービスにおける動作を適用する機能です。 ボリュームと同様にして、config はコンテナー内のファイルシステムにおけるファイルとしてマウントされます。 コンテナー内のマウントポイントは、デフォルトで Linux コンテナーの場合 /<config名>、Windows コンテナーの場合 C:\<config名> です。

サービスから config にアクセスできるのは、トップレベル要素 services における configs 属性により明示的にアクセスが許可された場合だけです。

デフォルトにおいて、config は以下の特徴を持ちます。

  • config の所有者はコンテナー実行を行っているユーザーですが、サービス設定により上書き変更が可能です。
  • ワールド読み込みの権限 (モード 0444) を持ちますが、サービス設定により上書きされていればそれに従います。

トップレベル要素 configs による宣言は、Compose アプリケーション内のサービスに対して、設定データを定義したり参照したりするための定義を行うものです。 The source of the config is either file, environment, content, or external.

  • file: The config is created with the contents of the file at the specified path.
  • environment: The config content is created with the value of an environment variable. Introduced in Docker Compose version 2.23.1.
  • content: The content is created with the inlined value. Introduced in Docker Compose version 2.23.1.
  • external: If set to true, external specifies that this config has already been created. Compose does not attempt to create it, and if it does not exist, an error occurs.
  • name: The name of the config object in the container engine to look up. This field can be used to reference configs that contain special characters. The name is used as is and will not be scoped with the project name.

例1

<プロジェクト名>_http_config is created when the application is deployed, by registering the content of the httpd.conf as the configuration data.

configs:
  http_config:
    file: ./httpd.conf

Alternatively, http_config can be declared as external. Compose looks up http_config to expose the configuration data to relevant services.

configs:
  http_config:
    external: true

例2

External configs lookup can also use a distinct key by specifying a name.

The following example modifies the previous one to look up a config using the parameter HTTP_CONFIG_KEY. The actual lookup key is set at deployment time by the interpolation of variables, but exposed to containers as hard-coded ID http_config.

configs:
  http_config:
    external: true
    name: "${HTTP_CONFIG_KEY}"

例3

<project_name>_app_config is created when the application is deployed, by registering the inlined content as the configuration data. This means Compose infers variables when creating the config, which allows you to adjust content according to service configuration:

configs:
  app_config:
    content: |
      debug=${DEBUG}
      spring.application.admin.enabled=${DEBUG}
      spring.application.name=${COMPOSE_PROJECT_NAME}

例4

<project_name>_simple_config is created when the application is deployed, using the value of an environment variable as the configuration data. This is useful for simple configuration values that don’t require interpolation:

configs:
  simple_config:
    environment: "SIMPLE_CONFIG_VALUE"

If external is set to true, all other attributes apart from name are irrelevant. If Compose detects any other attribute, it rejects the Compose file as invalid.