compose: [BUG] service can't be used with `extends` as it declare `depends_on`

Description

When trying to bring up a compose project that makes use of extends feature, I am getting the following error:

service "nginx" can't be used with `extends` as it declare `depends_on`

Steps To Reproduce

  1. Have a docker-compose.yml project with extends, like this:
nginx:
    extends:
      file: ./nginx/compose.yml
      service: nginx
    container_name: "${SERVER_NAME:}-nginx"
    env_file: .env

and the nginx/compose.yml:

version: "3.9"

services:
  nginx:
    image: nginx:1
    restart: unless-stopped
    depends_on:
      - web_app
    volumes:
      - ./html:/var/www/html
  1. docker compose up

  2. See error: service “nginx” can’t be used with extends as it declare depends_on

Compose Version

2.24.6

Docker Environment

N/A

Anything else?

No response

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 33
  • Comments: 47 (10 by maintainers)

Commits related to this issue

Most upvoted comments

encountering this too. why is a breaking change being included in a patch release at all?

This seems to be a new restriction added in 2.24.6. We have an extends combined with depends_on and everything was working fine with 2.24.5 and previous versions. Not sure why this wasn’t flagged as a breaking change?

restriction already was implemented in 2.23.x, new compose parser used in 2.24.x prior to latest release was just missing this check.

will be part of tomorrow’s release

This makes reusing blocks much harder, as fragments cannot be used across files. As far as I know, using extends was the only way to have reusable depends_on blocks while allowing to separate services in multiple files and have a common file to extend from.

@ndeloof please take a look how many linked issues this change produced. I vote for reverting this behavior. Мy company also has tens of projects with currently broken depends_on

This issue was closed two weeks ago as not planned.

Shouldn’t it be reopened, now that the relevant restriction in compose-spec has been lifted?

Extending depends_on was working before this change.

Yeah, same for me. Was working fine on 2.24.5 and broke on this new 2.24.6 release.

@matthiasPOE no, we relaxed this restriction in the spec https://github.com/compose-spec/compose-spec/pull/470 issue is closed by release v2.26.0

@meriouma maybe you want to check out include: https://docs.docker.com/compose/compose-file/14-include/

That is not sufficient. It is common to do stuff like have an image like “background worker” and then have 3 instances of it - high-pri, medium-pri, and low-pri. You could then have the base depend on stuff like your DB and message bus and such, then just have 3 instances that extend the base. Now you have to repeat those dependencies over and over in the instantiations. This is definitely more degradation in compose.

We have used Compose for development purposes with a Kubernetes cluster for deployment. It deeply feels like Compose has been losing functionality while Kubernetes has been gaining it, and Kind is becoming more and more compelling to replace Compose from these sorts of things.

Hello there, do we have an ETA for the version 2.25 to be released on Docker Desktop? My company heavily depends on this fix, for now I workaround the issue by download docker-compose instead and working with it, but the entire company is trained on the platform to use docker compose - would be nice to have the fix in place.

@davidlougheed compose-go/v2 is not “not-even-prod-ready”. We released as RC as we anticipated the need for some additional refactoring, and don’t want third party library to suffer from API changes.

THAT BEING SAID: as compose-go includes a consistency check, it would be able to detect reference to another service is orphaned after being used by extends, such a check docker compose v1 didn’t implemented (so this rule in the compose file format, inherited by the compose specification). With the updated architecture of compose-go/v2 we can now relax this constraint, and offer more flexibility to users (which still should read the docs before they complain about a feature :trollface:). I’ll write a proposal in the compose-spec repo for this purpose, so we can get this both fixed in the docs and in the code

@beni0888 you didn’t link the video 🙂

Any idea about when the fix will be released in OSX Docker Desktop?

@r3nor Try now, latest published version fixed it for me (2.25.0).

$ docker compose version
Docker Compose version v2.25.0
$ docker --version
Docker version 26.0.0, build 2ae903e

Thanks @ndeloof !

Got this error with ubuntu update

from docker-compose-plugin (2.24.6-1~ubuntu.22.04~jammy) to (2.24.5-1~ubuntu.22.04~jammy)

You’re right! Fixed!!

In case someone else is facing issues because Docker Desktop is still running an old version of the docker-compose plugin, I’ve found this useful video explaining how to easily upgrade the docker-compose plugin used by Docker Desktop.

should happen in a few days

Thanks @ndeloof for fixing the spec.

The change broke my project as I have the base docker-compose.yaml which declares my API server (which depends_on a database image) built from the project source (ie: compiling code) and a .devcontainer/docker-compose-dev.yaml which extends the base service, but changes the build.target so that an image is used that has all the developer dependencies and mounts the source files.

This used to work so that the developer image/container would still depend on the DB which worked well for the development team.

BTW I don’t think you were able to overwrite depends_on before. What you happen is that their depends_on would be merged with yours.

I want to reuse another developers service without making them change their code. If I ‘include’ I am stuck with their implementation. why can’t I just overwrite with my own ‘depends_on’ and move on?

The problem with this approach is that if more than one service derives from the base service (and this is always the case — why would you go into the effort of sharing functionality if there is just one service), you will need to copy-paste the “depends_on” stanza into each one. Also, if there is more than one service in the derivation chain, it will make the derived services unusable on their own, i.e. consider a situation where you have service “x" extending service “y" which extends service “z”, all of which depend on the service “foo”. This change moves the “depends_on foo” logic from z to x. Before this change, you could happily start a container with services z and y, while after the change, only service x is usable.

On Feb 28, 2024 at 4:41:39 AM, luciangabor @.***> wrote:

It’s possible to merge depends_on “later”:

$ find *.yaml -print -exec cat {} ;compose.yamlinclude: - path: - defs.yaml - deps.yamldefs.yamlservices: x: image: x y: image: y

depends_on:

- x

z: extends: ydeps.yamlservices: y: depends_on: - x

$ docker compose configname: depsservices: x: image: x networks: default: null “y”: depends_on: x: condition: service_started required: true image: “y” networks: default: null z: image: “y” networks: default: nullnetworks: default: name: deps_default

— Reply to this email directly, view it on GitHub https://github.com/docker/compose/issues/11544#issuecomment-1968591617, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAFYDJ2SWSFO7HK2ERSK3VDYV33VHAVCNFSM6AAAAABDZYY2JKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSNRYGU4TCNRRG4 . You are receiving this because you were mentioned.Message ID: @.***>

… and if you want to “inherit” the depends_on, deps.yaml could be like this:

services:
  y: &idem
    depends_on:
      - x
  z: *idem