docker / compose

Define and run multi-container applications with Docker
https://docs.docker.com/compose/
Apache License 2.0
33.53k stars 5.17k forks source link

[BUG] include and !override do not work correctly in nested include scenario #12029

Closed eldiaboloz closed 1 month ago

eldiaboloz commented 1 month ago

Description

I am trying to use nested includes and it seems the !override option is not working as expected. I have read a similar issue and tried the same approach, but it doesnt work if there are multiple level of includes

Steps To Reproduce

create 4 yaml files: main.yaml:

include:
  - "./service-main.yaml"

extend.yaml:

include:
  - path:
      - "./service-main.yaml"
      - "./service-extend.yaml"

service-main.yaml:

services:
  test:
    image: "nginx"
    ports:
      - "127.0.0.1:80:80"
    environment:
      - VARA=test

service-extend.yaml:

services:
  test:
    ports: !override
      - "127.0.0.2:80:80"
    environment: !override
      - VARB=test

run docker config

docker container run -it -v $PWD:/test -w /test docker:latest \
  docker compose -f main.yaml -f extend.yaml config

expected output:

name: test
services:
  test:
    environment:
      VARB: test
    image: nginx
    networks:
      default: null
    ports:
      - mode: ingress
        host_ip: 127.0.0.2
        target: 80
        published: "80"
        protocol: tcp
networks:
  default:
    name: test_default

actual output:

name: test
services:
  test:
    environment:
      VARA: test
      VARB: test
    image: nginx
    networks:
      default: null
    ports:
      - mode: ingress
        host_ip: 127.0.0.1
        target: 80
        published: "80"
        protocol: tcp
      - mode: ingress
        host_ip: 127.0.0.2
        target: 80
        published: "80"
        protocol: tcp
networks:
  default:
    name: test_default

Compose Version

Docker Compose version v2.29.1

Docker Environment

No response

Anything else?

No response

ndeloof commented 1 month ago

AFAICT your expectation is wrong.

you combine main+ extends

the resulting model are merged, so you get VARA and VARB declared.

eldiaboloz commented 1 month ago

Thanks @ndeloof for the explanation. In the end I achieved what I needed with a compose.override.yaml symlink, which points to non-existent file by default. And when you put a file where the symlink is pointing to, the !override or !reset works as expected ( as they modify the service without new includes ). I didn't know that you can use a broken symlink with the .override file like this.

ndeloof commented 1 month ago

That's not a documented feature, you should not rely on this behavior which could be considered a bug