Docker多个相同的端口问题 [英] Docker multiple same port issue

查看:856
本文介绍了Docker多个相同的端口问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am currently working on two inter related ASP.NET Core WebAPI services (Service1 & Service2) in a solution. Both are having docker files and exposing port 80.

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

Service1 is an independent service and required to be called from Service2. I have given both docker-compose.yml and 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 rel = nofollow noreferrer> http:// localhost:1002 ,其中网络模式:网桥,我收到以下异常。

When i call http://localhost:1001 from http://localhost:1002 with network_mode: bridge, I am getting the below exception.


{System.Net.Http.HttpRequestException:无法分配请求的地址
---> System.Net.Sockets.SocketException:无法分配请求的地址

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

当我更改network_mode时主机( network_mode:host ),我得到以下异常。

when i change the network_mode to host ( network_mode:host ), i am getting the below exception.


System.IO.IOException: '无法绑定到地址 http:// [::]:80
地址已被使用。'

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

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

So please let me know how to resolve the issue.

推荐答案

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

You should stick with the default bridge mode (by removing network_mode: bridge) and change

  - SERVICE1=http://localhost:1001

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

由于service1和service2在单独的容器上,因此容器上没有打开的端口1001服务2。

Because service1 and service2 are on separate containers, so there is no opened port 1001 on the container of service2.

这篇关于Docker多个相同的端口问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆