How to extend service in Docker Compose V3?

血红的双手。 提交于 2019-12-22 09:39:19

问题


I have a docker-compose.yml which looks like

version: '2'
services:
  redis:
    image: redis

  mysqldb:
    image: mysql:5.7
    environment:
      - MYSQL_ROOT_PASSWORD=passme
      - MYSQL_DATABASE=mydb
      - MYSQL_USER=root

  base:
    build: .
    volumes:
      - .:/proj
    environment:
      - ENV_1=Value_1
      - ENV_2=Value_2
      - ENV_3=Value_3

  worker:
    extends:
      service: base
    command: celery -A proj worker --loglevel=debug
    links:
      - redis
      - mysqldb
    depends_on:
      - mysqldb

  web:
    extends:
      service: base
    links:
      - mysqldb
      - redis
    depends_on:
      - mysqldb
    ports:
      - "8000:8000"
    command: python manage.py runserver 0.0.0.0:8000


Now, I want to upgrade it to version-3.

From the doc

The extends keyword is supported in earlier Compose file formats up to Compose file version 2.1 (see extends in v1 and extends in v2), but is not supported in Compose version 3.x



So, here is my question, How can I use Version-3 docker-compose file without losing my current functionalities?


回答1:


Extending services isn't supported in version 3 after removal of extends.

As for workaround, you can use docker-compose addons, e.g.

include:
    - http://example.com/compositions/servicea.yaml
    - http://example.com/compositions/serviceb.yaml

namespace: core

web:
    image: example/service_a:latest
    links: ['servicea.web', 'serviceb.api']

Another way is to include multiple composer files from the command-line (with multiple -f options). See: Add support for multiple composefile when deploying. E.g.

docker-compose -f submodules/A_SUB_PROJECT/docker-compose.yml -f submodules/B_SUB_PROJECT/docker-compose.yml config | docker-compose -f - up

Source: When using multiple docker-compose.yml files from different directories, local paths are not followed correctly.


In general, version 3 is more ideal for Swarm/Kube stacks, so consider using it.

E.g.

docker stack deploy -c docker-compose.yml mystack1
docker stack deploy -c another/docker-compose.yml mystack2

If you're not using Swarm or Docker Enterprise Kubernetes stacks, there is no reason to use v3. Stick with v2.4, and you get all the docker-compose cli features including extends, depends_on, extension fields, and even depends_on with healthchecks (to avoid wait-for-it scripts).


Related:

  • Include external config
  • feature: including external docker-compose.yml
  • Add support for extends feature in Compose v3 / docker stack deploy
  • extends not supported in version 3 schema



回答2:


The document in docs.docker.com community is so clear on what changes were made from 2.x to 3.x and what need to be done to upgrade it; I request you make use of those link to upgrade the docker-compose file. I am sharing the link for your information here and search for "Version 2.x to 3.x" and more over the document also talks about what needs to be done for the keyword extend; search for "Extending services" in the link.



来源:https://stackoverflow.com/questions/52587643/how-to-extend-service-in-docker-compose-v3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!