asp.net - 无法使用 docker-compose 连接到 postgresql

标签 asp.net postgresql docker docker-compose

我遇到的问题是找不到主机或连接到此主机时出错

我尝试了许多不同的选项,但尚未找到问题的解决方案。

我的 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/

相关文章:

python - SQLAlchemy 还是 psycopg2?

mysql - docker nodejs容器无法连接mysql容器

php - Docker 应用程序无法访问在另一个容器中运行的 MySQL

asp.net - 如何通过 JQuery/Javascript 调用保护我的 Web 服务?

c# - Entity Framework 4 到 6 的解决方法

c# - ASP :textbox select all text by click button and copy to client clipboard

ASP.NET MVC 3 静态文件简单场景授权

sql - 将架构添加到postgresql中的路径

postgresql 序列跳转 30 或 33 数字缓存等于 1

Git 在 Docker 容器中运行太慢