【问题标题】:Angular CLI proxy one Docker container to another, bridge network, ECONNREFUSEDAngular CLI 将一个 Docker 容器代理到另一个,桥接网络,ECONNREFUSED
【发布时间】:2021-12-29 18:28:50
【问题描述】:

我有一个 Angular 应用程序在一个 VsCode 开发容器中运行,一个 ASP Core 后端在另一个 VsCode 开发容器中运行。我不想处理 CORS 的混乱,我想对 Angular 开发服务器进行 api 调用,然后将调用代理到 ​​ASP Core 后端。

我的 Docker 网络配置是这样的:

C:\Users\Martin>docker network inspect bridge
[
    {
        "Name": "bridge",
        "Id": "90dcb78bded2417bba81f8ff71e232462eb23c4d72ecbfbfd8862beaafb7780e",
        "Created": "2021-12-27T10:10:23.2279842Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.17.0.0/16",
                    "Gateway": "172.17.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "44ae270de9b06f74f4c24daca75cf35ddb8beedb4d44e131881655b3669027b5": {
                "Name": "romantic_robinson",
                "EndpointID": "625a6feb4acabff027c164fce9fd3c1c0f3bfa35e8fabbfd8f1da668d3baf151",
                "MacAddress": "02:42:ac:11:00:02",
                "IPv4Address": "172.17.0.2/16",
                "IPv6Address": ""
            },
            "e30c03da9479509aba2a2ab2b3ecd423e2679547fb72c86529428b317d503b73": {
                "Name": "stupefied_goldberg",
                "EndpointID": "20b1c9c3495b6b7941177a46fabf329d39b6b6058460839c7ea96bef06aa3bd7",
                "MacAddress": "02:42:ac:11:00:03",
                "IPv4Address": "172.17.0.3/16",
                "IPv6Address": ""
            }
        },
        "Options": {
            "com.docker.network.bridge.default_bridge": "true",
            "com.docker.network.bridge.enable_icc": "true",
            "com.docker.network.bridge.enable_ip_masquerade": "true",
            "com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
            "com.docker.network.bridge.name": "docker0",
            "com.docker.network.driver.mtu": "1500"
        },
        "Labels": {}
    }
]

我的 Angular 代理配置是这样的:

{
   "/api": {
      "target": "http://172.17.0.2:5000",
      "secure": false,
      "logLevel": "debug",
      "pathRewrite": {
         "^/api": ""
       }
   }
}

我原以为这会导致 Angular 代理将请求转发到 ASP Core 容器,而不是报告这个

[HPM] Rewriting path from "/api/account/user-data" to "/account/user-data"
   [HPM] GET /api/account/user-data ~> http://172.17.0.1:5000
   [HPM] Error occurred while trying to proxy request /account/user-data from
   localhost:4200 to http://172.17.0.1:5000 (ECONNREFUSED) (https://nodejs.org
   /api/errors.html#errors_common_system_errors)

我尝试过使用localhost ASP Core docker bridge IP 172.17.0.2 和 Docker Host IP 172.17.0.1,但没有任何效果。我得到ECONNREFUSED 的所有信息。

我想不出还有什么可以尝试的。

【问题讨论】:

  • 我认为你需要在 "/api" 之后更改原点 :{}, "changeOrigin": true
  • 我试过没有效果。

标签: angular docker docker-networking


【解决方案1】:

Properties\launchSettings.json 中我不得不更改 ASP 服务

  // "applicationUrl": "https://localhost:5001;http://localhost:5000",
  "applicationUrl": "https://0.0.0.0.:5001;http://0.0.0.0:5000",

这会导致监听服务监听 Docker 容器中所有可用的网络接口,而不仅仅是localhost

这也有效

  "applicationUrl": "https://docker0:5001;http://docker0:5000",

我认为这让我对今年创建的每一个项目都感到震惊!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-14
    • 1970-01-01
    • 1970-01-01
    • 2020-07-05
    • 1970-01-01
    • 2017-02-26
    • 1970-01-01
    • 2018-07-28
    相关资源
    最近更新 更多