我遇到的问题是找不到主机或连接到此主机时出错
我尝试了许多不同的选项,但尚未找到问题的解决方案。
我的 docker-compose 文件
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:
我的 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"]
连接字符串 -
"Host=database;Database=DbTest;Username=admin;Password=superpassworddocker;Port=5432"
最佳答案
我解决了我的问题,只需将连接字符串中的 Host=database
更改为 Host=host.docker.internal
即可正常工作
关于asp.net - 无法使用 docker-compose 连接到 postgresql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74778732/