![](/img/trans.png)
[英]Trying to connect C# ASP.NET MVC app to a docker container running postgres and I keep getting "password authentication failed"
[英]docker-compose up C# ASP.NET MVC app container along side of a postgresql container Failed to connect to [::1]:5432
I am trying to launch my C# ASP.NET MVC app along side of a postgres server in 2 respective docker containers running on the same docker network. 它行不通。 以下是相关代码。
appsettings.Development.json:
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
},
"ConnectionStrings": {
"db": "Host=localhost;Database=amaranth_tests;Username=amaranth_test_user;Password=postgrespw123;Port=5432"
}
}
appsettings.json:
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"db": ""
},
"ApiEndpoints": {
"TestNet": [
"https://api.bitcore.io/api/BTC/testnet/",
"https://api.blockcypher.com/v1/btc",
"<API KEY HERE>"
],
"MainNet": [
"https://api.bitcore.io/api/BTC/mainnet/",
"https://api.blockcypher.com/v1/btc",
"<API KEY HERE>"
],
"db": "Host=localhost;Database=amaranth_tests;Username=amaranth_test_user;Password=postgrespw123;Port=5432",
}
}
docker-compose.yml:
version: '3.8'
volumes:
data:
networks:
api-dev:
driver: bridge
services:
postgresql:
image: postgres
# explicit container name
container_name: postgresql
environment:
- POSTGRES_USER=amaranth_test_user
- POSTGRES_PASSWORD=postgrespw123
- POSTGRES_DB=amaranth_tests
ports:
- 5432:5432
volumes:
- data:/var/lib/postgresql
networks:
- api-dev
amaranth_main:
container_name: amaranth_main
links:
- postgresql
depends_on:
- postgresql
build:
context: .
dockerfile: Dockerfile
networks:
- api-dev
ports:
- "8000:80"
command: ["./wait-for-postgres.sh", "postgresql", "dotnet", "amaranth.dll"]
Dockerfile:
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build-env
WORKDIR /app
# Copy everything
COPY . ./
# Restore as distinct layers
ENV PATH $PATH:/root/.dotnet/tools
RUN dotnet tool install -g dotnet-ef --version 6.0.8
RUN dotnet restore
RUN dotnet ef database update
RUN dotnet publish -c Release -o out
# Build runtime image
FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "amaranth.dll"]
#!/bin/sh
# wait-for-postgres.sh
set -e
host="$1"
shift
until PGPASSWORD=$POSTGRES_PASSWORD psql -h "$host" -U "postgres" -c '\q'; do
>&2 echo "Postgres is unavailable - sleeping"
sleep 1
done
>&2 echo "Postgres is up - executing command"
exec "$@"
包括实际应用程序在内的所有上述文件都位于应用程序amaranth
的根目录中。
问题是当我运行docker-compose up
,我在终端中收到此错误。
#10 6.663 Failed to connect to [::1]:5432
它在 Dockerfile RUN dotnet ef database update
中的这一步失败。 从终端看来,它似乎没有等待 postgresql 容器在 ASP.NET 应用程序开始运行之前完成启动,即使我添加了depends_on:
参数和"./wait-for-postgres.sh"
命令到amaranth_main
( docker-compose 文件中的 ASP.NET 容器)。 那么为什么这不起作用呢?
我尝试使用Host=postgresql
而不是Host=localhost
,我得到了#13 6.506 Name or service not known
问题在于连接字符串,在容器网络内它无法连接本地主机,因为它会尝试在容器内找到本地主机,如果是 docker-compose,您需要使用服务名称来访问 postgres 容器,在您的情况下它是“postgresql” .
所以你在 appsettings.json 中的连接字符串应该是
db="Host=postgresql;Database=amaranth_tests;Username=amaranth_test_user;Password=postgrespw123;Port=5432",
或者,您可以将 docker-compose 文件中的连接字符串传递给 amaranth_main 作为
environment:
- ConnectionStrings__db=Host=postgresql;Database=amaranth_tests;Username=amaranth_test_user;Password=postgrespw123;Port=5432
希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.