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
I solved my problem, just change
Host=database
in the connection string toHost=host.docker.internal
and it works fineShould the database name be
TestDb
instead ofDbTest
in your connection string?