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.