在 docker compose for django application 中使用 YAML 扩展字段时错误映射键必须是唯一的

Error Map Keys Must be unique while using YAML extension field in docker compose for django application

嗨,我的 YAML 文件中有这个,我在 django 服务的 <<: *common 处收到错误 Map keys must be unique

version: '3.4'

x-common: &common
  restart: unless-stopped
  networks: docker_django

x-django-build: &django-build
  build:
    context: .
    dockerfile: ./DockerFile.dev

services:

  django:
    <<: *django-build
    <<: *common
    container_name: docker_django_dc01
    command: bash -c "python manage.py runserver 0.0.0.0:8000"
    ports:
      - 8000:8000
    volumes:
      - ./:/code
    depends_on:
      - postgres

<<: *reference 实际上不是关键字;这是一个普通的 YAML 映射条目,其键 << 恰好有特殊处理。关于 SO,请参阅 ; that in turn refers to the Merge Key Language-Independent Type for YAML™ Version 1.1 规范草案。

由于 << 是一个映射键,尝试将映射转换为字典的 YAML 解析器不希望看到它的多个。相反,规范允许将序列作为值,因此对于这种特定用途,您可以编写

<<: [*django-build, *common]

(还要考虑您是否需要这么多自定义设置。为您编写 provides 一个名为 default 的网络,这样您通常不需要 networks:;如果您只是命名您的Dockerfile,嗯,Dockerfile 然后你可以使用 one-liner build: . 这将避免在这里需要 YAML alias/anchor 语法。)