docker 如何将REST API容器的URL配置到另一个具有Angular CLI代理的容器中

oxf4rvwz  于 2023-04-05  发布在  Docker
关注(0)|答案(1)|浏览(127)

我有一个Web应用程序,它是用Angular 7编写的,带有代理和.NET Core 2.2作为WebAPI。我的问题是,本地的一切都可以工作,但我无法从Codker容器内的WebAPI发出任何请求,因为我得到了

craftsmen.client_1  | [HPM] Rewriting path from "/api//CraftOffer/GetManyWithFilters/0/5?category=0" to "/Public//CraftOffer/GetManyWithFilters/0/5?category=0"
craftsmen.client_1  | [HPM] GET /api//CraftOffer/GetManyWithFilters/0/5?category=0 ~> https://localhost:5000
craftsmen.client_1  | [HPM] Rewriting path from "/api/CraftOffer/GetCategories" to "/Public/CraftOffer/GetCategories"
craftsmen.client_1  | [HPM] GET /api/CraftOffer/GetCategories ~> https://localhost:5000
craftsmen.client_1  | [HPM] Error occurred while trying to proxy request /Public//CraftOffer/GetManyWithFilters/0/5?category=0 from localhost:4200 to https://localhost:5000 (ECONNREFUSED) (https://nodejs.org/api/errors.html#errors_common_system_errors)
craftsmen.client_1  | [HPM] Error occurred while trying to proxy request /Public/CraftOffer/GetCategories from localhost:4200 to https://localhost:5000 (ECONNREFUSED) (https://nodejs.org/api/errors.html#errors_common_system_errors)

如何在Docker容器中从客户端请求WebAPI?
我试过了

  • 更改以将proxy.conf.json内部的目标更改为https://craftsmen.webapi:5000
  • ASPNETCORE_URLS从http更改为https
  • 更改以将proxy.conf.json内部的目标更改为https://127.0.0.1:5000

当前proxy.conf.json

{
    "/api/*": {
      "target": "https://localhost:5000",
      "secure": false,
      "logLevel": "debug",
      "changeOrigin": true,
      "pathRewrite": {"api" : "Public"}
    }
  }

当前Angular应用的Dockerfile

# source https://mherman.org/blog/dockerizing-an-angular-app/
# base image
FROM node:12.2.0

# install chrome for protractor tests
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
RUN sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list'
RUN apt-get update && apt-get install -yq google-chrome-stable

# set working directory
WORKDIR /app

# add `/app/node_modules/.bin` to $PATH
ENV PATH /app/node_modules/.bin:$PATH

# install and cache app dependencies
COPY package.json /app/package.json
RUN npm install
RUN npm install -g @angular/cli@7.3.9

# add app
COPY . /app

# start app
CMD ng serve --host 0.0.0.0

停靠-编写.yml

version: '3'

services:
  craftsmen.webapi:
    build:
      context: .
      dockerfile: BackEnd/src/Craftsmen.WebApi/Dockerfile
    ports:
      - "5000:5000"
    volumes:
      - webapi:/webapi
    networks:
      - craftsmenservices_network
  craftsmen.client:
    build:
      context: ./FrontEnd
      dockerfile: Dockerfile
    volumes:
      - 'FrontEnd:/app'
      - '/app/node_modules'
    ports:
      - '4200:4200'
    networks:
      - craftsmenservices_network
    depends_on:
      - craftsmen.webapi
    links:
      - craftsmen.webapi
  craftsmenservices_network:
    driver: bridge
volumes:
  webapi:
  FrontEnd:

当前WebApi的Dockerfile

FROM microsoft/dotnet:2.2-runtime AS base
WORKDIR /app
ENV ASPNETCORE_URLS https://*:5000

FROM microsoft/dotnet:2.2-sdk AS build
WORKDIR /src
COPY BackEnd/src/Craftsmen.WebApi/Craftsmen.WebApi.csproj src/Craftsmen.WebApi/
COPY BackEnd/src/Craftsmen.Infrastructure/Craftsmen.Infrastructure.csproj src/Craftsmen.Infrastructure/
COPY BackEnd/src/Craftsmen.Core/Craftsmen.Core.csproj src/Craftsmen.Core/
COPY BackEnd/src/Craftsmen.Contract/Craftsmen.Contract.csproj src/Craftsmen.Contract/
COPY BackEnd/src/Craftsmen.Database/Craftsmen.Database.csproj src/Craftsmen.Database/
RUN dotnet restore src/Craftsmen.WebApi/Craftsmen.WebApi.csproj
COPY ./BackEnd .
WORKDIR /src/src/Craftsmen.WebApi
COPY BackEnd/src/Craftsmen.WebApi/entrypoint.sh .
RUN dotnet build Craftsmen.WebApi.csproj -c Release -o /app
RUN chmod +x ./entrypoint.sh
CMD /bin/bash ./entrypoint.sh

我希望能够从Angular客户端向WebApi发出成功的请求

km0tfn4u

km0tfn4u1#

快速回答

你不应该在docker容器中使用localhost。你可以用途:

  • 本地ip
  • 公共IP
  • 公有领域
    脏溶液:使用ip代替localhost

由于你使用的是docker-compose,你所有的docker应用都在同一个主机上,所以在你的linux shell中执行这个命令:

hostname -I

获取IP并在proxy.conf.json中使用,而不是localhost

长答案

此外,还应强调:docker-composedocker network仅用于开发目的,不适用于真实的企业场景。

相关问题