Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend a base service from the same file #1988

Closed
bfirsh opened this issue Sep 5, 2015 · 27 comments
Closed

Extend a base service from the same file #1988

bfirsh opened this issue Sep 5, 2015 · 27 comments

Comments

@bfirsh
Copy link

bfirsh commented Sep 5, 2015

It is a common pattern to define a base service which you want several services to extend, but it's currently very verbose to do this:

docker-compose-base.yml

base:
  environment:
    FOO: bar

docker-compose.yml

web:
  extends:
    file: docker-compose-base.yml
    service: base
  ...
db:
  extends:
    file: docker-compose-base.yml
    service: base
  ...

It should be possible to extend an incomplete service in a single compose file. Something like this:

base:
  abstract: true
  environment:
    FOO: bar
web:
  extends:
    service: base
  ...
db:
  extends:
    service: base

Design decisions:

  • How should a service be marked as a service that shouldn't be run? (I've used "abstract" in the example above for the sake of illustration, pinched from Django's "abstract" models.)
mrfuxi added a commit to mrfuxi/compose that referenced this issue Sep 11, 2015
Signed-off-by: Karol Duleba <mr.fuxi@gmail.com>
@dnephin
Copy link

dnephin commented Sep 11, 2015

It is a common pattern to define a base service which you want several services to extend,

Do you have some examples of this? I've never seen it myself, and I'm having a hard time understanding why you would ever need this?

If you want to share a bunch of common environment you can use an env_file, or even a data volume container with volumes_from.

@dnephin
Copy link

dnephin commented Sep 17, 2015

I think supporting abstract services might be a mistake. The only requirement for a service is to have an image, which is a pretty easy requirement to satisfy. You can always just use small base image to satisfy it (ex: alpine:edge). Supporting abstract services adds extra complexity that I think we can avoid.

@bfirsh
Copy link
Author

bfirsh commented Sep 24, 2015

@dnephin The problem is it also has to run when you do docker-compose up. Sort of related to this too: #1896

@dnephin
Copy link

dnephin commented Sep 24, 2015

You can always set a command of true, sh, echo to just have it run basically a no-op. We fixed the behaviour in 1.4 so that one container exiting doesn't cause the whole stack to shutdown.

@aanand
Copy link

aanand commented Sep 24, 2015

Actually, I think that fix lands in 1.5.

@j-a-m-l
Copy link

j-a-m-l commented Nov 3, 2015

Currently I am using YAML node anchors, but this proposal could be more useful.

That works this way:

web: &piwik-base
  container_name: piwik-web
  image: piwik-image
  restart: always
  ...

cron:
  <<: *piwik-base
  container_name: piwik-cron
  command: cron -f

When extending other file, it also requires the file key, like this thread initial example.

@dnephin
Copy link

dnephin commented Nov 4, 2015

This is now in the 1.5.0 release. The rest is covered by #1896

@dnephin dnephin closed this as completed Nov 4, 2015
@dnephin
Copy link

dnephin commented Nov 4, 2015

@j-a-m-l yaml nodes and anchors are another option. I've used them myself, but not everyone is a fan.

There's an older PR for adding them to the docs: #1015

@amanica
Copy link

amanica commented Feb 4, 2016

I'd like to have a common base that includes dns and dns_search options, will look at the YAML node anchors workaround for now.

@bfirsh
Copy link
Author

bfirsh commented Mar 17, 2016

Reopening because @fxdgear has run into this issue running some internal Docker stuff.

We should either:

  1. Document the recommended way of doing this currently (using command: /bin/true?)
  2. Implement this as a feature and document it

My concern is that using a fake command is a hack – and isn't obvious when reading the Compose file what the intent of that is. I think (1) would work as a stop gap, but I think we should probably do (2) at some point.

@bfirsh bfirsh reopened this Mar 17, 2016
@bfirsh
Copy link
Author

bfirsh commented Mar 18, 2016

Also another idea from @fxdgear: maybe these things could live under a different key? Example above would look like:

base-services:
  base:
    environment:
      FOO: bar
services:
  web:
    extends:
      service: base
    ...
  db:
    extends:
      service: base

The reason I dislike this is it would mean having a new top-level key for each type of object we add to Compose files (networks, volumes, etc).

@dunk
Copy link

dunk commented Mar 29, 2016

I think that abstract base services are one half of the problem. They cover the case where you have services which are very similar, sharing most of their defined attributes. However, many services still need to share common subsets of attributes (frequently if there is a one-to-many relationship between some set of containers - e.g. a database or a queue - where all client services will share configuration). In that case, a single abstract base class doesn't buy you much. There's a longer ticket with more discussion over here if you are interested: #3167.

@wclr
Copy link

wclr commented Nov 15, 2016

So, what is the current state, is it possible to extend service from current docker-compose.yml?

@jamshid
Copy link

jamshid commented Jun 26, 2017

@whitecolor idk but I think extends itself eventually became deprecated.

The recommended replacement, where you specify multiple compose yml files in COMPOSE_FILE and they are merged, doesn't really satisfy this use case.

In docker-compose 1.14.0 how do people reuse common configuration of a service when defining other services?

@wclr
Copy link

wclr commented Jun 26, 2017

@jamshid probably not typical approach for a medium project I prefer to use dynamic docker-compose config that is produced from JS (TypeScript) configuration. But it works much better than dancing around docker-compose.yml format, yaml anchors, etc.

@bfirsh
Copy link
Author

bfirsh commented Jul 15, 2017

Specifying multiple Compose files doesn't satisfy this use case. (I ran into this problem again today.)

@shin-
Copy link

shin- commented Feb 15, 2018

Addressed in #5140

@shin- shin- closed this as completed Feb 15, 2018
@p7k
Copy link

p7k commented Aug 8, 2018

Sorry, I still don't quite see what solution is now (from #5140). Could someone point me to the docs?

@shin-
Copy link

shin- commented Aug 8, 2018

https://docs.docker.com/compose/compose-file/#extension-fields

@Fi1osof
Copy link

Fi1osof commented Nov 3, 2020

2020 year.
Still waiting for...

@shinsenter
Copy link

2021

@kylr42
Copy link

kylr42 commented Dec 16, 2021

2022

@gfranxman
Copy link

2023

@xurvan
Copy link

xurvan commented May 11, 2023

2023 😢

@izakdvlpr
Copy link

2023

@dfyz011
Copy link

dfyz011 commented Jan 9, 2024

2024

@chardskarth
Copy link

chardskarth commented Feb 13, 2024

@dfyz011 @izakdvlpr (and the rest)

Isn't this achievable already (since 2017) via Extension Fields?

Using the example above, you could do

x-base-service: &base-service
  environment:
    FOO: bar
    
web:
  <<: *base-service
  ...
db:
  <<: *base-service

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests