> For the complete documentation index, see [llms.txt](https://camelgemonion.gitbook.io/docker/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://camelgemonion.gitbook.io/docker/dockercompose.yaml-wen-jian-zhong-chang-yong-zhi-ling/untitled-12/untitled-21.md).

# secrets configuration reference

顶级secrets声明定义或引用了能够用于stack的服务的授权的secrets。secrets既可以是file也可以是external。

* file：将使用指定路径中的文件内容创建secret。
* external：如果设置为true，则指定此secret已创建。 Docker不会尝试创建它，如果它不存在，则会发生secret未找到错误。
* name: Docker中的secret对象的名称。此字段可用于引用包含特殊字符的secret。该名称按原样使用，不会与stack名称在一个作用域。以3.5版文件格式引入。
* template\_dirver: 要使用的模板驱动程序的名称，它控制是否以及如何将secret有效负载评估为模板。如果未设置驱动程序，则不使用任何模板。当前唯一支持的驱动程序是golang。以3.8版文件格式引入，仅在使用docker stack时受支持。

在这个示例中，当stack部署的时候，my\_first\_secret会被创建为\<stack\_name>\_my\_first\_secret, my\_second\_secret已经存在于Docker中。

```
secrets:
  my_first_secret:
    file: ./secret_data
  my_second_secret:
    external: true
```

外部secrets的另一个变体是Docker中的secret名称与服务中存在的名称不同。以下示例修改了前一个示例，以使用名称为redis\_secret的外部secret。

#### Compose 文件 v3.5和以上 <a href="#compose-file-v35-and-above" id="compose-file-v35-and-above"></a>

```
secrets:
  my_first_secret:
    file: ./secret_data
  my_second_secret:
    external: true
    name: redis_secret
```

#### Compose File v3.4和以下 <a href="#compose-file-v34-and-under" id="compose-file-v34-and-under"></a>

```
 my_second_secret:
    external:
      name: redis_secret
```

在stack中，我们仍然需要为每一个服务进行secrets授权。
