简体   繁体   中英

docker-compose identical service names in different docker-compose files collision

There are 2 services with 2 docker-compose.yml files. The services are connected via bridge network, so that they can communicate locally.

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

But when I am trying to request one service from another, for example send request to service_2 from service_1:

curl http://django:8000 It is not reachable.

netcat -vz django 8000 shows that it is not reachable as well.

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.

Is it possible to resolve without renaming service to a unique one in 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.

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 .

So in order to connect these two, you should use either of these two names in each container:

service_1_django
service_2_django

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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