Home > front end >  docker-compose up C# ASP.NET MVC app container along side of a postgresql container Failed to connec
docker-compose up C# ASP.NET MVC app container along side of a postgresql container Failed to connec

Time:08-31

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. It won't work. The following is the relevant code.

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 "$@"

All of the above files including the actual app are in the root directory of the app amaranth.

The problem is that when I run docker-compose up, I get this error in my terminal.

#10 6.663 Failed to connect to [::1]:5432

It fails on this step in the Dockerfile RUN dotnet ef database update. From the terminal it appears that it didn't wait for postgresql container to finish being initiated before the ASP.NET app started running, even though I added the depends_on: parameter and the "./wait-for-postgres.sh" command to amaranth_main (the ASP.NET container) in the docker-compose file. So why isn't this working?

Also

I tried using Host=postgresql instead of Host=localhost, and I got #13 6.506 Name or service not known

CodePudding user response:

The Problem is with connectionstring, inside the container network it can't connect localhost, as it will try to find localhost within container, in case of docker-compose you need to use servicename to reach postgres container, which in your case it is "postgresql".

so your connection string in appsettings.json should be

db="Host=postgresql;Database=amaranth_tests;Username=amaranth_test_user;Password=postgrespw123;Port=5432",

Alternatively you can pass the connectionstring in docker-compose file for amaranth_main as

environment:
      - ConnectionStrings__db=Host=postgresql;Database=amaranth_tests;Username=amaranth_test_user;Password=postgrespw123;Port=5432

Hope it helps.

updated ans based on further discussion:

RUN dotnet ef database update this needs to have database running but during image generation it is not running in your case. so it is better to do migration inside code. You can remove database update from dockerfile and perform within the code during startup. that's the best practice followed generally. also

you can check this link. it has detail article on how to do migration on strartup.

  • Related