KamilMastalerz
KamilMastalerz

Reputation: 235

How to configure the url of a rest api container into another container which has Angular cli proxy

I have Web application which is written in Angular 7 with proxy and .NET Core 2.2 as WebAPI. My problem is that locally everything works but I'm unable to make any request from WebAPI inside Codker container because I get

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)

How to be able to make requests for WebAPI from client inside Docker container?

I've tried:
- change to change target inside proxy.conf.json into https://craftsmen.webapi:5000
- changing ASPNETCORE_URLS from http into https
- change to change target inside proxy.conf.json into https://127.0.0.1:5000

Current proxy.conf.json

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

Current Dockerfile for Angular app

# 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/[email protected]

# 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:

Current Dockerfile For WebApi

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

I expect to be able to make succesfull requests from Angular Client to WebApi

Upvotes: 1

Views: 2096

Answers (1)

JRichardsz
JRichardsz

Reputation: 16505

Quickly answer

You should not use localhost inside a docker container. You could use:

  • local ip
  • public ip
  • public domain

Dirty solution: Use ip instead localhost

SInce you are using docker-compose, all your docker apps are in the same host,so execute this in your linux shell:

hostname -I

To get the ip and use it in proxy.conf.json instead localhost

Long Answer

Also, to emphasize: docker-compose and docker networks is just for development purposes not for real enterprises scenarios.

Upvotes: 2

Related Questions