C#C
C#17mo ago
Thiago R.

Local machine refuses to connect to an ASP.NET Core Web API container

Hello

I need to find out how to access my Docker container via the host machine. My connection is refused, no matter the tool I use (browser,
curl
, wget, javascript's fetch). I've been looking for a solution since yesterday, but no success at all.
I've been trying to instantiate two containers within a same docker network, which is basically an ASP.NET Core Web API and a PostgreSQL server.

I have 0 Docker knowledge at all. I was using Rider's feature for instantiating containers and it worked properly. However, I decided to use a
docker-compose.yml
file instead, because I was editing the same environment variables in both containers (such as PostgreSQL credentials and a default user seed data). I've deleted the IDE configuration before I actually start to build the
docker-compose.yml
file as follows:

version: '3.8'

services:
  webapi:
    image: trsaints-frontend-api
    build:
      context: .
      dockerfile: Dockerfile
    environment:
      - JWT_SINGING_KEY=${JWT_SINGING_KEY}
      - JWT_ISSUER=${JWT_ISSUER}
      - JWT_AUDIENCE=${JWT_AUDIENCE}
      - DB_HOST=db
      - DB_PORT=5432
      - DB_NAME=${DB_NAME}
      - DB_USER=${DB_USER}
      - DB_PASSWORD=${DB_PASSWORD}
      - ALLOWED_HOSTS=${ALLOWED_HOSTS}
      - ALLOWED_CORS_DOMAINS=${ALLOWED_CORS_DOMAINS}
      - ADMIN_USERNAME=${ADMIN_USERNAME}
      - ADMIN_PASSWORD=${ADMIN_PASSWORD}
      - ASPNETCORE_URLS=http://localhost:8080
    ports:
      - "8082:8080"
      - "8081:8081"
    depends_on:
      - db
    networks:
      - trsaints-network

  db:
    image: postgres:alpine3.20
    environment:
      - POSTGRES_DB=${DB_NAME}
      - POSTGRES_USER=${DB_USER}
      - POSTGRES_PASSWORD=${DB_PASSWORD}
    ports:
      - "5433:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
    networks:
      - trsaints-network

volumes:
  postgres_data:

networks:
  trsaints-network:


Both containers run properly, I just can't connect to the api container externally.
Was this page helpful?