使用Docker Compose的微服务:相同的容器,多个项目 [英] Micro Services With Docker Compose: Same Container, Multiple Projects

查看:99
本文介绍了使用Docker Compose的微服务:相同的容器,多个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与其他一些人一起,我在使用应用程序的微服务体系结构并以我想要的方式使用docker-compose时遇到了问题.

Along with a few others, I am having issues using a micro services architecture of applications and employing docker-compose the way I want to.

摘要: 我有X个微服务项目(可将其称为project Aproject Bproject C.每个微服务都依赖于相同的容器(可将其称为这些dependency Ddependency E.

Summary: I have X micro service projects (lets call these project A, project B and project C. Each micro service depends on the same containers (lets call these dependency D and dependency E.

问题: 理想情况下,项目ABC将在其docker-compose.yml文件中具有 ALL 两个依赖项(D& E);但是,这成为一个问题,因为docker compose将这些容器视为 duplicate 容器,而实际上,我想重用它们.这是常见的错误消息:

The Problem: Ideally, project A, B and C would ALL have both dependencies (D & E) in their docker-compose.yml files; however, this becomes an issue as docker compose sees these as duplicate containers when in reality, I would like to reuse them. Here is an error message that is commonly seen:

错误:对于A无法为服务A创建容器:b'冲突.这 容器"sha"已在使用容器名称"/A".你必须 删除(或重命名)该容器,以便能够重复使用该名称.'

ERROR: for A Cannot create container for service A: b'Conflict. The container name "/A" is already in use by container "sha". You have to remove (or rename) that container to be able to reuse that name.'

据我所见,人们建议您在一个项目中定义容器并使用 >和外部链接.尽管这可行,但它会引入对另一个docker-compose yml文件(定义依赖项的文件!)的依赖项.

From what I have seen, people are recommending that you define the container in one project and reference it using networks and external links. Although this works, it introduces a dependency on a different docker-compose yml file (the file that defines the dependency!).

我读过的另一种方法主张将容器隔离在其docker组合文件中,然后在要构建时引用多个文件.再说一次,尽管这可行,但它肯定不像docker通常使用的那样令人惊讶地方便.如果我无法解决问题,我会采用这种方法.

Another approach that I've read argues for isolation of containers in their docker compose files and then referencing multiple files when you want to build. Again, although this works, its certainly not as stunningly convenient as docker typically is to work with. If I am unable to work out a solution, I will go with this approach.

非单一回购世界中的其他人(特别是微服务)是否有其他方法取得成功?

Have other people in the non-mono repo world (specifically with micro services) had any success with a different approach?

有人要求我举例说明:

这是project Aproject B的2个不同的yml文件的外观:

Here is what 2 different compose yml files look like for project A and project B:

项目A:

version: '2'
services:
  dependencyD:
    image: dependencyD:latest
    container_name: dependencyD

  dependencyE:
    image: dependencyE:latest
    container_name: dependencyE

  projectA:
    image: projectA:latest
    container_name: projectA
    depends_on:
      - dependencyD
      - dependencyE

项目B:

version: '2'
services:
  dependencyD:
    image: dependencyD:latest
    container_name: dependencyD

  dependencyE:
    image: dependencyE:latest
    container_name: dependencyE

  projectB:
    image: projectB:latest
    container_name: projectB
    depends_on:
      - dependencyD
      - dependencyE

推荐答案

有一个名为

There is a feature called external links. From the docs:

链接到在docker-compose.yml之外甚至在Compose之外启动的容器,特别是对于提供共享或公共服务的容器.

Link to containers started outside this docker-compose.yml or even outside of Compose, especially for containers that provide shared or common services.

具有多个docker-compose.yml文件对于将容器组织成有意义的组也是很常见的.也许您的方案可以使用多个YAML文件和外部链接.

Having multiple docker-compose.yml files is also common to organize containers into meaningful groups. Maybe your scenario can use multiple YAML files and the external links.

这篇关于使用Docker Compose的微服务:相同的容器,多个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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