-2

I have created a Spring Boot project connected to PostgreSQL. When running the project and connecting from my IDE, the PostgreSQL I installed within Docker is working fine.

I use Docker Desktop version 4.6.1 on Windows 11, and I have faced two problems when starting docker.

First:

docker run --name myapp --network=host -d 220d023bd426

For the command above, the application is starting, but I can't access it from the outside like Postman, then I try

docker exec -it 099583cc05b6 /bin/bash

and use curl to test it works.

Second:

docker run --name myapp -p 8181:8080 -d 220d023bd426

For command above, it shows an error:

org.postgresql.util.PSQLException: Connection to 127.0.0.1:6543 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP

Zeitounator
  • 38,476
  • 7
  • 53
  • 66
Theara
  • 79
  • 2
  • 12
  • 2
    please show exact `curl` commands that succeed, what are images `220d023bd426` and `099583cc05b6` – rok Apr 23 '22 at 07:27
  • This one 220d023bd426 a image and 099583cc05b6 a container id – Theara Apr 23 '22 at 10:08
  • The CURL that I test on container: `curl --location --request GET 'http://localhost:8181/emp?empNo=102298' \ --header 'emp;' \ --header 'Content-Type: application/json' \ --data-raw '{ "percentage_range": 86 }'` – Theara Apr 23 '22 at 10:08
  • How are you starting the database? How are you configuring the connection to it? In Docker `localhost` usually means "the current container"; you might look at other questions like [How to communicate between Docker containers via "hostname"](https://stackoverflow.com/questions/30545023/how-to-communicate-between-docker-containers-via-hostname) for some ideas on standard Docker networking. – David Maze Apr 23 '22 at 10:36
  • @DavidMaze the database I install as a docker, I use application.properties for connecting to the database, `spring.datasource.url=jdbc:postgresql://127.0.0.1:6543/demo` if I run on IntelliJ, it's working fine. the problem when I run as a docker show connection confused with the second command. Note both the database and application I run as the Docker. – Theara Apr 23 '22 at 12:30
  • That is trying to connect to port 6543 in your Spring application container. The database is in a different container, which is not `localhost` or `127.0.0.1`. [Docker Compose + Spring Boot + Postgres connection](https://stackoverflow.com/questions/44790923/docker-compose-spring-boot-postgres-connection) has another example. – David Maze Apr 23 '22 at 12:50
  • @DavidMaze I follow sample docker-compose it works, but I still can't test from Postman using IP 172.26.0.3 and 127.0.0.1 docker inspect `"IPAddress": "", "IPPrefixLen": 0, "IPv6Gateway": "", "MacAddress": "", "Networks": { "spring-boot-data_default": { "Aliases": [ "springapp", "40136a92fc9d" ], "Gateway": "172.26.0.1", "IPAddress": "172.26.0.3",` – Theara Apr 23 '22 at 14:12
  • The `docker inspect` IP address doesn't work in a wide variety of situations, and it's never necessary to look it up. From a browser or a tool like Postman you should usually be able to connect to `localhost` (or the host's IP address or DNS name) and the first published `ports:` number. – David Maze Apr 23 '22 at 14:25
  • Thank you @DavidMaze now I can test from Postman. – Theara Apr 23 '22 at 14:30

1 Answers1

-1

You need to add the --network=host to the second command as well, otherwise it is trying to connect to the 127.0.0.1 of the container. But please make sure you understand what network=host means

Sergio Santiago
  • 1,316
  • 1
  • 11
  • 19
  • Host networking doesn't work on Windows or MacOS. If it works, it disables Docker's networking stack, which you usually don't want (there's a suggestion that they've remapped their database port, for example, which isn't an option with host networking). – David Maze Apr 23 '22 at 10:34