Can't connect to postgresql db which is running on windows host from a spring app which is running in a docker container. I get Error response from daemon: invalid IP address in add-host: "host.docker.internal"
docker-compose.yml
version: '3.8'
services:
spring-app-container:
image: spring-app:1
build:
context: ./
dockerfile: Dockerfile
ports:
- "50800:50800"
extra_hosts:
- "local:host.docker.internal"
environment:
- DATABASE_HOST=local
- DATABASE_USER=user
- DATABASE_PASSWORD=********
- DATABASE_NAME=psqldb
- DATABASE_PORT=5432
application.properties
## PostgreSQL
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/psqldb
spring.datasource.username=user
spring.datasource.password=******
Is it correct to have localhost as url parameter in the spring application.properties?
In the postgresql.conf, listen_addresses = '*'. Is this sufficient to allow the connection from spring app in the container?
I saw this approach on stackoverflow:
IP_ADDRESS=$(ip addr show | grep "\binet\b.*\bdocker0\b" | awk '{print $2}' | cut -d '/' -f 1)
Where can I add this command in the docker-compose.yml file?
and in the docker-compose.yml
extra_hosts:
docker.host: ${IP_ADDRESS}`
Thanks in advance