skip to Main Content

I am facing the problem the host is not found or an error connecting to this hosting

I have tried many different options but have not found a solution to the problem.

My docker-compose file

version: '3.4'

services:
  backend:
    image: webapiwithdockerpostgre
    build:
      context: .
      dockerfile: WebApiWithDockerPostgre/Dockerfile

  database:
    image: postgres
    restart: always
    ports:
        - 5432:5432
    environment:
        POSTGRES_USER: admin
        POSTGRES_PASSWORD: superpassworddocker
        POSGRES_DB: TestDb
    volumes:
      - database-data:/var/lib/postgresql/data/


volumes:
    database-data:
    pgadmin:

My Dockerfile

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["WebApiWithDockerPostgre/WebApiWithDockerPostgre.csproj", "WebApiWithDockerPostgre/"]
RUN dotnet restore "WebApiWithDockerPostgre/WebApiWithDockerPostgre.csproj"
COPY . .
WORKDIR "/src/WebApiWithDockerPostgre"
RUN dotnet build "WebApiWithDockerPostgre.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "WebApiWithDockerPostgre.csproj" -c Release -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WebApiWithDockerPostgre.dll"]

Connection string –

"Host=database;Database=DbTest;Username=admin;Password=superpassworddocker;Port=5432"

2

Answers


  1. Chosen as BEST ANSWER

    I solved my problem, just change Host=database in the connection string to Host=host.docker.internal and it works fine


  2. Should the database name be TestDb instead of DbTest in your connection string?

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search