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

3
  • 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. Commented Jul 16, 2022 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.) Commented Jul 16, 2022 at 15:50
  • i solved it replacing"host.docker.internal" and with the ip of the container obtained from cmd : Commented Jul 28, 2022 at 15:38

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.