0

i am trying to write a crud api just for training , and when i come to db part , i've preferred to use a docker postgres container instead of installing the whole postgres project . but the problem is my api that is not containerzed and the postgres container cannot connect with each other this is the postgres container creation cmd : network :

sudo docker network create --driver bridge crud_db_network_connection

postgres container :

sudo docker run --name postgresDbCrudContainer -p 5432:5432 -e POSTGRES_PASSWORD=postgres -e POSTGRES_USER=postgres -e POSTGRES_DB="cruddb" --network crud_db_network_connection postgres

my api application.yml :

spring:
  datasource:
    url: jdbc:postgresql://host.docker.internal:5432/cruddb
    username: postgres
    password: postgres
    driver-class-name: org.postgresql.Driver
  jpa:
    database: POSTGRESQL
    show-sql: true
    hibernate:
      ddl-auto: create

(hint : the api is in my real system it's not in a container .) how should i do to consume my db from the spring api .

i've solved this issue but i will let this problem for every one facing the same issue :

i solved it replacing "host.docker.internal" and with the ip of the container obtained from cmd : sudo docker network inspect network_name

Drago Ban
  • 105
  • 1
  • 8
  • 2
    `host.docker.internal` doesn't exist outside of Docker; if your app is outside Docker it can't use that. Since you used `-p` to bind your Postgres to port 5432, use `localhost` in your datasource url instead. – Dan Lowe Jul 16 '22 at 14:36
  • (The second `-p` number must be the standard port 5432, but the first number can be anything that's not otherwise in use on your host, and your application settings need to match the first port number.) – David Maze Jul 16 '22 at 15:50
  • i solved it replacing"host.docker.internal" and with the ip of the container obtained from cmd : – Drago Ban Jul 28 '22 at 15:38

0 Answers0