Docker 多个相同端口问题

标签 docker asp.net-core

我目前正在一个解决方案中处理两个相互关联的 ASP.NET Core WebAPI 服务(Service1 和 Service2)。两者都有 docker 文件并公开端口 80。

Service1是一个独立的服务,需要从Service2调用。我已经给出了 docker-compose.yml 和 docker-compose.override.yml。

docker-compose.yml

version: '3.4'

    services:
      services1:
        image: ${DOCKER_REGISTRY-}services1
        network_mode: bridge
        build:
          context: .
          dockerfile: Services1/Dockerfile
        ports:
         - "501:80"

      services2:
        image: ${DOCKER_REGISTRY-}services2
        network_mode: bridge
        build:
          context: .
          Services2/Dockerfile
        ports:
         - "502:80"

docker-compose.override.yml

version: '3.4'

services:
  services1:
    build:
      context: .
      dockerfile: Services1/Dockerfile
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
    ports:
      - "1001:80"

  services2:
    build:
      context: .
      dockerfile: Services2/Dockerfile
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - SERVICE1=http://localhost:1001
      ports:
      - "1002:80"    

当我调用 http://localhost:1001来自 http://localhost:1002使用 network_mode: bridge,我得到以下异常。

{System.Net.Http.HttpRequestException: Cannot assign requested address ---> System.Net.Sockets.SocketException: Cannot assign requested address

当我将 network_mode 更改为主机(network_mode:host)时,出现以下异常。

System.IO.IOException: 'Failed to bind to address http://[::]:80: address already in use.'

所以请让我知道如何解决这个问题。

最佳答案

您应该坚持使用默认桥接模式(通过删除 network_mode: bridge)并更改

  - SERVICE1=http://localhost:1001

  - SERVICE1=http://services1:80 # services1 will be resolved by docker

因为service1和service2在不同的容器上,所以service2的容器上没有开放1001端口。

关于Docker 多个相同端口问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54103725/

相关文章:

Laravel 和 Docker 权限问题 - 无法保存文件

docker - 下一个JS : Prevent env vars to be required on build time

c# - 在不使用控制台 readline 或 readkey 的情况下,将自托管 servicestack 服务作为 docker swarm 服务打开

go - golang执行 'docker run..'异常退出

使用通配符 SSL 证书在 AWS E2 上部署 Reactjs 应用程序

asp.net-core - .NET Core 更改我的默认 index.html 文件所在的位置

c# - ASP.NET 核心 DI : Resolve same instance if scoped service is registered both as service type and implementation type

docker - Docker 上 Quarkus 和 Keycloak 的配置

mysql - 从带有代理的 CORE 连接到 Google Cloud SQL 实例 : A network-related or instance-specific error occurred. 我可以通过 mysql cmd 连接

c# - 状态 cookie 无效。处理远程登录时遇到错误。 ASP.NET Core MVC 外部社交登录