Home:ALL Converter>ASP Dotnet Core MVC Docker connection to Postgresql

ASP Dotnet Core MVC Docker connection to Postgresql

Ask Time:2021-03-14T00:16:55         Author:danielr

Json Formatter

I've been breaking my head over an issue I have when Dockerizing my ASP Dotnet application. I've been going through StackOverflow without any success.

Basically what I am trying to achieve is to run my ASP dotnet application (MVC API) in a Docker-container. I also want my Postgresql instance to run in a container in the same network.

I've created a docker-compose file like so:

version: '3'
networks:
  asp-dotnet-network:
    driver: bridge

services:
  postgres:
    image: postgres:9.6.21-alpine
    container_name: postgres
    ports:
      - "5433:5432"
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_DB=postgres
    networks:
      - asp-dotnet-network
      
  app:
    build: ../.
    container_name: app
    ports:
      - "5000:5000"
      - "5001:5001"
    environment:
      - ConnectionStrings__Default=Host=postgres;Username=postgres;Port=5432;Password=postgres;Database=postgres;Pooling=true;
    depends_on:
      - postgres
    networks:
      - asp-dotnet-network

My Dockerfile looks like the following

FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /app

COPY *.sln .

COPY Data/Data.csproj ./Data/
COPY Application/Application.csproj ./Application/
COPY WebApi/WebApi.csproj ./WebApi/

RUN dotnet restore

COPY Data/. ./Data/
COPY Application/. ./Application/
COPY WebApi/. ./WebApi/

WORKDIR /app/WebApi
RUN dotnet publish -c Release -o out

FROM mcr.microsoft.com/dotnet/sdk:5.0 AS runtime
WORKDIR /app

COPY --from=build /app/WebApi/out ./
ENTRYPOINT ["dotnet", "WebApi.dll"]

Lastly, I have configured my main application to accept environment variables so that I can override the appsettings:

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); })
                .ConfigureAppConfiguration((hostingContext, config) =>
                {
                    config.AddJsonFile("appsettings.json").
                        AddJsonFile($"appsettings.docker.json", true).AddEnvironmentVariables();
                });

When I run docker-compose up --build && docker-compose logs -f in my directory that contains the docker-compose.yml file, the following error occurs in the app container:

app         | fail: Microsoft.EntityFrameworkCore.Database.Connection[20004]
app         |       An error occurred using the connection to database 'postgres' on server 'tcp://postgres:5432'.
app         | fail: Microsoft.EntityFrameworkCore.Query[10100]
app         |       An exception occurred while iterating over the results of a query for context type 'DatabaseContext'.
app         |       System.InvalidOperationException: An exception has been raised that is likely due to a transient failure.
app         |        ---> Npgsql.NpgsqlException (0x80004005): Exception while connecting
app         |        ---> System.Net.Sockets.SocketException (111): Connection refused

At first I thought this was an issue with the networks, but when I execute a ping command in my app-container, I can find the postgres container. I also tried to install psql in the app-container, and even then I can access my postgres database.

I'm quite clueless. Does anyone have an idea what's the issue?

Author:danielr,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/66615770/asp-dotnet-core-mvc-docker-connection-to-postgresql
danielr :

This answer on stackoverflow fixed it for me, the database didn't boot up completely.",
2021-03-14T12:16:07
yy