简体   繁体   English

docker-compose 相同服务名称在不同 docker-compose 文件冲突

[英]docker-compose identical service names in different docker-compose files collision

There are 2 services with 2 docker-compose.yml files.有 2 个服务有 2 个docker-compose.yml文件。 The services are connected via bridge network, so that they can communicate locally.这些服务通过桥接网络连接,因此它们可以在本地进行通信。

  1. service_1 docker-compose.yml service_1 docker-compose.yml
version: '3.7'

services:
    django:
        build:
            context: .
        ports:
            - "6000:6000"
        networks:
            - shared-local-network
        command: python manage.py runserver 0.0.0.0:6000

networks:
    shared-local-network:
        external: true
  1. service_2 docker-compose.yml service_2 docker-compose.yml
version: '3.7'

services:
    django:
        build:
            context: .
        ports:
            - "8000:8000"
        networks:
            - shared-local-network
        command: python manage.py runserver 0.0.0.0:8000

networks:
    shared-local-network:
        external: true

So those services are in different projects, but their service names are the same: django .所以这些服务在不同的项目中,但它们的服务名称是相同的: django

But when I am trying to request one service from another, for example send request to service_2 from service_1:但是当我尝试从另一个服务请求一项服务时,例如从 service_1 向 service_2 发送请求:

curl http://django:8000 It is not reachable. curl http://django:8000无法访问。

netcat -vz django 8000 shows that it is not reachable as well. netcat -vz django 8000表明它也无法访问。

I noticed that when sending request from service_1 to http://django:8000 , it sends request to itself service_1, that's why it cannot find opened 8000 port, because service_1 has 6000 opened port.我注意到当从 service_1 向http://django:8000发送请求时,它向自己的 service_1 发送请求,这就是它找不到打开的 8000 端口的原因,因为 service_1 有 6000 个打开的端口。

Is it possible to resolve without renaming service to a unique one in docker-compose.yml ?是否可以在不将服务重命名为docker-compose.yml中的唯一服务的情况下解决问题?

Let us suppose you have these two paths:让我们假设您有这两条路径:

/home/service_1/
/home/service_2/

And service_1 's docker-compose is in its same name dir, and service_2 as well.并且service_1docker-compose在其同名目录中,并且service_2也是如此。

When you run service_1 's docker-compose , the running container name is service_1_django and when you run service_2 's docker-compose , the running container name is service_2_django .运行service_1docker-compose时,运行容器名称为service_1_django ,运行service_2docker-compose时,运行容器名称为service_2_django

So in order to connect these two, you should use either of these two names in each container:因此,为了连接这两者,您应该在每个容器中使用这两个名称中的任何一个:

service_1_django
service_2_django

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM