Problem
I have an API in asp.net core and I'm using Npgsql with it which is running on docker. When I'm running it on visual studio with swagger it works, but if i'll put my api into container it can't connect to the postgres.
The problem comes up when I send a request to an endpoint.
What have I tried?
I've checked most of the posts. I think its related with my connection string. I've tried everything. any of the ip adresses nor the container names worked out.
Configs
Here is my appsettings.json in asp
"ConnectionStrings": {
"PostgresDatabase": "Host=local_pgdb;Port=5432; Database=postgres; Username=user; Password=admin;MultipleActiveResultSets=true"
},
here are my docker files.
postgres docker file
version: "3.8"
services:
db:
image: postgres
container_name: local_pgdb
restart: always
ports:
- "5432:5432"
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: admin
volumes:
- local_pgdata:/var/lib/postgresql/data
pgadmin:
image: dpage/pgadmin4
container_name: pgadmin4_container
restart: always
ports:
- "5050:80"
environment:
PGADMIN_DEFAULT_EMAIL: [email protected]
PGADMIN_DEFAULT_PASSWORD: admin
volumes:
- pgadmin-data:/var/lib/pgadmin
volumes:
local_pgdata:
pgadmin-data:
asp api dockerfile
FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base
WORKDIR /app
ENV ASPNETCORE_ENVIRONMENT Development
ENV ASPNETCORE_URLS=http://+:80
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src
COPY ["haymatlosApi.csproj", "."]
RUN dotnet dev-certs https
RUN dotnet restore "./haymatlosApi.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "haymatlosApi.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "haymatlosApi.csproj" -c Release -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
EXPOSE 80/tcp
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "haymatlosApi.dll"]
Problem in the cmi in docker.
warn: Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware[3]
2023-08-23 17:45:53 Failed to determine the https port for redirect.
2023-08-23 17:45:55 warn: Microsoft.EntityFrameworkCore.Query[10102]
2023-08-23 17:45:55 The query uses a row limiting operator ('Skip'/'Take') without an 'OrderBy' operator. This may lead to unpredictable results. If the 'Distinct' operator is used after 'OrderBy', then make sure to use the 'OrderBy' operator after 'Distinct' as the ordering would otherwise get erased.
2023-08-23 17:45:55 warn: Microsoft.EntityFrameworkCore.Query[10102]
2023-08-23 17:45:55 The query uses a row limiting operator ('Skip'/'Take') without an 'OrderBy' operator. This may lead to unpredictable results. If the 'Distinct' operator is used after 'OrderBy', then make sure to use the 'OrderBy' operator after 'Distinct' as the ordering would otherwise get erased.
enter code herefail: Microsoft.EntityFrameworkCore.Database.Connection[20004]
2023-08-23 16:56:31 An error occurred using the connection to database 'postgres' on server ''.
2023-08-23 16:56:31 fail: Microsoft.EntityFrameworkCore.Query[10100]
2023-08-23 16:56:31 An exception occurred while iterating over the results of a query for context type 'haymatlosApi.haymatlosApi.Models.PostgresContext'.
2023-08-23 16:56:31 System.InvalidOperationException: An exception has been raised that is likely due to a transient failure.
2023-08-23 16:56:31 ---> Npgsql.NpgsqlException (0x80004005): Failed to connect to [::1]:5432
2023-08-23 16:56:31 ---> System.Net.Sockets.SocketException (99): Cannot assign requested address
local_pgdb
works only within the same network, in your case, network created by your docker compose. Add asp.net application in docker compose or use docker run with --network flag.