使用Docker运行多个项目,每个项目都使用docker-compose运行 [英] Running multiple projects using docker which each runs with docker-compose

查看:489
本文介绍了使用Docker运行多个项目,每个项目都使用docker-compose运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用微服务方法来构建我们的产品.我们正在使用一些项目,每个项目都使用docker-compose来运行.问题在于,在开发环境中,如果要更改多个项目中的代码并测试开发的代码,则必须单独运行项目并手动将它们链接在一起.

We are using microservices approach to build our product. We are using some projects which each uses docker-compose to run. The problem is that in development environment, if we want to change codes in multiple projects and test developed codes, we must run projects separately and link them together manually.

现在,我们要创建一个开发套件,该套件可以克隆项目并一起运行它们并处理链接. docker-compose可以处理多个docker-compose文件吗?如果没有,有没有足够的工具为我们做?还是有针对我们目标的推荐方法?

Now we want to create a development kit which clones projects and runs them together and handles links. Can docker-compose handle multiple docker-compose file? If not is there any sufficient tool to do that for us? Or is there any recommended approach for our goal?

例如,我们有两个项目:PROJECT_A和PROJECT_B.每个人都有自己的docker-compose.yml,每个人都需要postgresql才能运行.像这样在PROJECT_A中有docker-compose.yml:

For example we have two projects: PROJECT_A and PROJECT_B. Each one has its own docker-compose.yml and each one needs postgresql to run. We have docker-compose.yml in PROJECT_A like this:

db:
    image: postgres:9.4
    ports:
      - "5432"

project_a:
    build: .
    command: python2.7 main.py
    links:
        - db

我们在PROJECT_B中有这样的docker-compose.yml:

And we have docker-compose.yml in PROJECT_B like this:

db:
    image: postgres:9.4
    ports:
      - "5432"

project_b:
    build: .
    command: python2.7 main.py
    links:
        - db

每个项目可以单独运行,并且可以正常运行.但是,如果要更改PROJECT_A和PROJECT_B之间的api,则需要运行两个项目并将它们链接在一起以测试我们的代码.现在,我们要编写一个开发工具包项目,该项目可以运行两个项目,并在需要时链接它们.最好的方法是什么?

Each project can run separately and works fine. But if we want to change the api between PROJECT_A and PROJECT_B we need to run both projects and link them together to test our code. Now we want to write a development kit project which can run both projects and link them if needed. What is the best approach to do this?

推荐答案

您可以使用docker-compose的="nofollow noreferrer"> extends 功能.将您的项目放在定义明确的位置,并使用相对路径引用它们:

You can do this by combining services from multiple files using the extends feature of docker-compose. Put your projects in some well-defined location, and refer to them using relative paths:

../
├── foo/
│   └── docker-compose.yml
└── bar/
    └── docker-compose.yml

foo/docker-compose.yml:

base:
    build: .

foo:
    extends:
        service: base
    links:
        - db

db:
    image: postgres:9

如果您想单独测试该项目,则可以执行以下操作:

If you wanted to test this project by itself, you would do something like:

sudo docker-compose up -d foo

创建foo_foo_1

Creating foo_foo_1

bar/docker-compose.yml:

foo:
    extends:
        file: ../foo/docker-compose.yml
        service: base
    links:
        - db

bar:
    build: .
    extends:
        service: base
    links:
        - db
        - foo

db:
    image: postgres:9

现在您可以同时测试两种服务:

Now you can test both services together with:

sudo docker-compose up -d bar

创建bar_foo_1
创建bar_bar_1

Creating bar_foo_1
Creating bar_bar_1

这篇关于使用Docker运行多个项目,每个项目都使用docker-compose运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆