Multiple Volume declaration leads to data loss

i’ve made typo, I declared Volume multiple times

Volume doesn’t save data

version: '3'

services:
  alex:
    container_name: mytest
    image: nginx
    volumes:
      - mydata:/data
    restart: always
    network_mode: "host"
    volumes:
      - "/etc/my.cnf:/my.cnf"

volumes:
  mydata:
    driver: local

With that conf Volume works fine

version: '3'

services:
  alex:
    container_name: mytest
    image: nginx
    restart: always
    network_mode: "host"
    volumes:
      - mydata:/data
      - "/etc/my.cnf:/my.cnf"

volumes:
  mydata:
    driver: local
# rpm -qa | grep docker
docker-ce-cli-20.10.11-3.el7.x86_64
docker-ce-20.10.11-3.el7.x86_64
docker-scan-plugin-0.9.0-3.el7.x86_64
docker-ce-rootless-extras-20.10.11-3.el7.x86_64

# docker-compose version
docker-compose version 1.24.0, build 0aa59064
docker-py version: 3.7.2
CPython version: 3.6.8
OpenSSL version: OpenSSL 1.1.0j  20 Nov 2018

First I didn’t understand what your question or porblem is since an incorrect yaml would obviously have incorrect result, but I guess this is a bug report, because Docker Desktop doesn’t warn you about the mistake. There is an old issue (maybe there are newer too) which explains why it was left that way:

I personally don’t agree with the explanation, because I tried yamllint docker-compose.yml with the example below

services:
  base-service: &service
    image: docker-image
    environment:
      ENV: "prod"

  service:
    <<: *service
    environment:
      ENV: "dev"

and it did not show the duplicate key as error, but it threw an error when I did not use anchors, so t could be handled.

On the other hand, you can make a habbit of using yamllint after you change a yaml file. If you are using an IDE, that probably supports to check them automatically while you edit them.

https://yamllint.readthedocs.io/en/stable/