16

hi I have this in my YAML file , and I am getting error Map keys must be unique at <<: *common in django service.

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
umair mehmood
  • 529
  • 2
  • 5
  • 17

1 Answers1

38

<<: *reference isn't actually a keyword; it's an ordinary YAML mapping entry with the key << that happens to have special treatment. On SO, see What is the << (double left arrow) syntax in YAML called, and where's it specced?; that in turn refers to the Merge Key Language-Independent Type for YAML™ Version 1.1 draft specification.

Since << is a mapping key, YAML parsers that try to convert mappings into dictionaries won't like to see multiple of it. Conversely, the spec allows a sequence as the value, so for this specific use you could write

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

(Also consider whether you need this many custom settings. Compose provides a network named default for you so you don't typically need networks:; if you just name your Dockerfile, well, Dockerfile then you can use a one-liner build: . That would avoid needing the YAML alias/anchor syntax here.)

David Maze
  • 130,717
  • 29
  • 175
  • 215