Micro Services With Docker Compose: Same Container, Multiple Projects

爱⌒轻易说出口 提交于 2019-12-22 18:14:44

问题


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.

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.

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:

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.'

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!).

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?


I've been ask to clarify with some examples:

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

Project 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

Project 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

回答1:


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

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

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.



来源:https://stackoverflow.com/questions/52378551/micro-services-with-docker-compose-same-container-multiple-projects

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