0

I'm trying to use docker compose to connect my ASP.Net Core web api to my postgres database.

Here is my docker-compose file

version: "3.9"

services:
  db:
    image: postgres:15.1-alpine
    container_name: db
    restart: always
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: "MyPassword123"
    ports:
      - "5432:5432"
    volumes:
      - db_data:/var/lib/postgresql/data
  api:
    image: ${DOCKER_REGISTRY-}api
    container_name: api
    
    environment:
      ASPNETCORE_ENVIRONMENT: Development
      ConnectionStrings_Default: Server=db;Port=5432;Database=DB;User Id=postgres;Password=MyPassword123;
    ports:
      - "7001:80"
    depends_on:
      - db
    build:
      context: .
      dockerfile: src/Services/Api/Dockerfile
      
volumes:
  db_data:

Here are the logs of my postgres DB

db   | 2023-01-19 09:46:17.887 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
db   | 2023-01-19 09:46:17.887 UTC [1] LOG:  listening on IPv6 address "::", port 5432
db   | 2023-01-19 09:46:17.906 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db   | 2023-01-19 09:46:17.921 UTC [50] LOG:  database system was shut down at 2023-01-19 09:46:17 UTC
db   | 2023-01-19 09:46:17.929 UTC [1] LOG:  database system is ready to accept connections

But on the API side I get the messsage:

Unhandled exception. Npgsql.NpgsqlException (0x80004005): Failed to connect to [::1]:5432

The db is correctly created and I can access it from my machine using PGAdmin using localhost and port 5432.

Should I create a user defined bridge network? I'm pretty sure everything should work like this.

Any help would be greatly appreciated.

1 Answer 1

2

The [::1] in the error message from the API is the IPv6 address for localhost. So it looks like it's not picking up the server name correctly from your connection string.

Looking at the docs, it seems like the keyword for the server is 'Host' and not 'Server', so try

ConnectionStrings_Default: Host=db;Port=5432;Database=DB;User Id=postgres;Password=MyPassword123;

If that doesn't help, then please provide more information on how you take the connection string and establish the connection to the database in C#.

Sign up to request clarification or add additional context in comments.

3 Comments

Silly me. Indeed the connection string expected a Host. Would you recommend creating a network in docker instead of using the default one?
The default network created by docker-compose usually works fine for me. I'd only create one in docker if the default one, for some reason, didn't satisfy my needs. I've never had to.
Found my Issue. My ConnectionString wasn't overrided by the "Environnement" section in the docker-compose file. I wasn't using double underscore _ _. So my connection string was picked up from the appsettings.json and pointing on localhost and not db.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.