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

时间:2019-11-09 16:46:39

标签: angular rest docker .net-core docker-compose

我有用Angular 7编写的Web应用程序,它具有Proxy和.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

docker-compose.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 Client向WebApi发出成功的请求

1 个答案:

答案 0 :(得分:1)

快速回答

您不能在docker容器内使用localhost。您可以使用:

  • 本地ip
  • 公共ip
  • 公共领域

肮脏的解决方案:使用ip代替localhost

在使用docker-compose时,所有docker应用都位于同一主机中,因此请在linux shell中执行此操作:

hostname -I

要获取ip并将其用于 proxy.conf.json 而不是localhost

长答案

还要强调: docker-compose 蚂蚁 docker网络仅用于开发