简体   繁体   English

docker-compose.networking 具有相同服务名称的多个应用程序

[英]docker-compose networking multiple apps with same service name

Problem:问题:

When having two docker-compose files / projects with the same services, under the same.network, when you spin up t he second compose project, the DNS name for the service gets overwritten.当有两个具有相同服务的 docker-compose 文件/项目时,在 same.network 下,当您启动第二个 compose 项目时,服务的 DNS 名称将被覆盖。

eg:例如:

App 1应用程序 1

version: "3.1"
services:
  db:
    image: mysql:8.0
    container_name: monolith-db
    networks:
      - my-network-name

  webserver:
    image: nginx:alpine
    container_name: monolith-webserver
    networks:
      - my-network-name

  phpfpm:
    container_name: monolith-phpfpm
    networks:
      - my-network-name
networks:
  my-network-name:
    external: true

App 2应用程序 2

version: "3.1"
services:
  db:
    image: mysql:8.0
    container_name: ms-auth-db
    networks:
      - my-network-name

  webserver:
    image: nginx:alpine
    container_name: ms-auth-webserver
    networks:
      - my-network-name

  phpfpm:
    container_name: ms-auth-phpfpm
    networks:
      - my-network-name
networks:
  my-network-name:
    external: true

If you start App 1, the services inside can connect to their declared services by service name as hostname, for example, in my config I have database-host: db However, when I do docker-compose -p ms-auth --env-file.env -f infra/local/docker-compose.yml up -d then db hostname now points to App 2's db service.如果启动 App 1,里面的服务可以通过服务名称作为主机名连接到它们声明的服务,例如,在我的配置中我有database-host: db但是,当我这样做时docker-compose -p ms-auth --env-file.env -f infra/local/docker-compose.yml up -d然后db hostname 现在指向 App 2 的 db 服务。

The solution is to use the container_name as hostname解决方案是使用container_name作为主机名

eg instead of connecting to db , configure App 1' config files to use the hostname monolith-db , and for pointing from App 1 to App 2, also use container name as hostname, eg ms-auth-host: ms-auth-webserver例如,不是连接到db ,而是将 App 1 的配置文件配置为使用主机名monolith-db ,并且为了从 App 1 指向 App 2,还使用容器名称作为主机名,例如ms-auth-host: ms-auth-webserver

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

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