Communication beetwen containers when send request

Hello.
I developed Rest API on ASP.net core and ASP MVC frontend.
I published 2 containers using docker compose(same network).
When I try to send request from frontend inside of docker I recieve error but if I try to send request from postman(containers are running), the request pass and return correct answer.

docker-compose

services:
  backend:
    build: 
      context: ./Backend/.
    ports:
    - '8080:80'
    container_name:
      backend 
    networks:
      - network1

  frontend:
    build: 
      context: ./Frontend/Tool/.
    ports:
    - '8081:80'
    container_name:
      frontend
    depends_on:
     - backend
    networks:
      - network1
networks:
  network1:
    driver: bridge

I guess the problem on frontend
private string apiUrl = $"http://localhost/api/vehicle";(local host I changed to 8080 but I had same problem)

var response = await _httpClient.PostAsJsonAsync(url, model);

Each container is a host, which means, it is its own localhost
When you call localhost from the frontend container, you reach the frontend

You can call other containers using service name or container name:
http://backend for example

1 Like

Thank you, it is solving the problem

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.