What am I doing wrong in docker-compose for .netcore and postgres?

1

I am banging my head for a while on this issue and can't find what the issue might be. Running Docker Desktop on Windows 10. I have one dotnetcore 3.1 api that connects to postgres. Both of these are being run in containers.

Everything seems to work except connection to the database. Since I looked at my docker-compose.yml milion times, I can't come up with any other idea.

Here is my connection string:

"Server=postgres;Port=5432;Database=IdentityManager;User Id=postgres;Password=12345678;"

Here is docker-compose.yml file:

version: '3'

services:
  identityserver:
    depends_on: 
      - "postgres"
    container_name: identityserver
    build:
      context: ./my_project/
      dockerfile: Dockerfile
    environment: 
      - ASPNETCORE_ENVIRONMENT='Development'
    ports: 
      - "5000:80"

  postgres:
    image: "postgres"
    container_name: "postgres"
    restart: always
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=12345678
      - POSTGRES_DB=IdentityManager
    expose: 
      - "5432"

Everything builds up, but connection to database fails:

Unhandled exception. Npgsql.NpgsqlException (0x80004005): Exception while connecting identityserver 
---> System.Net.Internals.SocketExceptionFactory+ExtendedSocketException (99): Cannot assign requested address [::1]:5432

The weirdest thing is that when I run postgres alone with this same configuration on docker-compose.yml, and run the application outside of container with slightly different connection string:

"Server=127.0.0.1;Port=5432;Database=IdentityManager;User Id=postgres;Password=12345678;"

I am able to connect to database.

I tried cleaning everything docker system prune -a, tried restarting Docker, restarting PC, but to no awail. Can anyone try to help?

postgresql
docker
docker-compose
asked on Stack Overflow May 1, 2020 by mrGreenBrown • edited May 1, 2020 by mrGreenBrown

2 Answers

0

try to

links:
  - postgres

Maybe it will help

answered on Stack Overflow May 1, 2020 by Giga Kokaia
0

Finally, I was able to resolve my own problem and it wasn't in the docker-compose.yml file at all. Somewhere in the application code, connection string was changed to look for localhost as a host instead of postgres.

After changing it back to postgres, everything was fine.

answered on Stack Overflow May 7, 2020 by mrGreenBrown

User contributions licensed under CC BY-SA 3.0