我如何在docker-compose中对容器进行分组?

时间:2016-08-23 11:55:03

标签: docker docker-compose

我正在使用这个docker-compose.yml。 如果可能的话,我想做更简单和继承的配置。

version: '2'

services:
    nginx-proxy:
        image: jwilder/nginx-proxy
        container_name: nginx-proxy
        ports:
            - "80:80"
        volumes:
            - /var/run/docker.sock:/tmp/docker.sock:ro

    web_one:
        container_name: "web_one"
        build:
            context: ./
            dockerfile: web.docker
        volumes:
            - ./../one:/var/www

        environment:
            - VIRTUAL_HOST=whoami_one.local
        links:
            - app_one
    app_one:
        container_name: "app_one"
        build:
            context: ./
            dockerfile: app.docker
        volumes:
            - ./../one:/var/www
        links:
            - db


    web_two:
        container_name: "web_two"
        build:
            context: ./
            dockerfile: web.docker
        volumes:
            - ./../two:/var/www

        environment:
            - VIRTUAL_HOST=whoami_two.local
        links:
            - app_two
    app_two:
        container_name: "app_two"
        build:
            context: ./
            dockerfile: app.docker
        volumes:
            - ./../two:/var/www
        links:
            - db

我有15个配置相同的网站。 我可以简化配置吗?像这样:

version: '2'

services:
    nginx-proxy:
        image: jwilder/nginx-proxy
        container_name: nginx-proxy
        ports:
            - "80:80"
        volumes:
            - /var/run/docker.sock:/tmp/docker.sock:ro

    one:
        extends:
            file: common-services.yml
        volumes:
            - ./../one:/var/www

        environment:
            - VIRTUAL_HOST=whoami_one.local

    two:
        extends:
            file: common-services.yml
        volumes:
            - ./../two:/var/www

        environment:
            - VIRTUAL_HOST=whoami_two.local

还是更好?

谢谢!

1 个答案:

答案 0 :(得分:2)

另一种方法是创建依赖于其他服务的无操作服务。 例如,在以下docker-compose.yml中,我有两个名称空间,dev用于开发应用程序时所需的服务,以及与可视化应用程序指标相关的服务指标(因为我对开发时不熟悉这些服务器感兴趣)。 / p>

version: "3"
services:
  dev:
    image: monroe/noop
    depends_on: ["postgres", "keycloak"]
  metrics:
    image: monroe/noop
    depends_on: ["grafana"]
  postgres: ...
  keycloak: ...
  grafana: ...