I know that my problem is an old shoe and this topic has already been answered in all questions but however I am running into a problem building a Docker container with a Spring Boot Application and just can't get ahead since 4 days. I have a local Postgresql database that I want to access with my container. However, I always get the error: org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
I went through every video on Youtube and with my friend "Chat..." I'm also just going around in circles.
This is my application.properties
server.port=8080
## Database konfiguration
spring.datasource.url= jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=admin
spring.jpa.hibernate.ddl-auto=validate
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.default_schema=invdr-webshop
# pretty format SQL
spring.jpa.properties.hibernate.format_sql=true
#spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQL81Dialect
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
# uncomment to show sql
spring.jpa.properties.hibernate.show_sql=true
# show bind parameters
logging.level.org.hibernate.type.descriptor.sql=DEBUG
allowed.origins=http://localhost:4200
spring.data.rest.base-path=/api
here is my Dockerfile:
FROM openjdk:17.0.2-jdk-slim
# Setzen der Arbeitsverzeichnis im Container
WORKDIR /app
# Kopieren des Spring Boot-JARs in den Container
COPY target/webshop-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-jar", "app.jar"]
EXPOSE 8080
When I start the jar I get no errors and everything works great. The database connection was also tested for the 1000th time and it works.
When I start the jar I get no errors and everything works great. The database connection was also tested for the 1000th time and it works.
With the command:
docker build -t test .
I create my Docker image and with
docker run --network=test:latest
I start the container. Here I tell the container that it can communicate with the outside world.
Here are my Postgres Settings
# - Connection Settings -
listen_addresses = '*' # what IP address(es) to listen on;
# comma-separated list of addresses;
# defaults to 'localhost'; use '*' for all
# (change requires restart)
port = 5432 # (change requires restart)
max_connections = 100 # (change requires restart)
#superuser_reserved_connections = 3 # (change requires restart)
#unix_socket_directories = '' # comma-separated list of directories
I have also checked my firewall. In the firewall settings I find the app Docker Desktop Backend under "Allowed Apps and Features" and it is accessible both privately and publicly. Is this sufficient?
docker network inspect bridge
[
{
"Name": "bridge",
"Id": "fccdfe4adbdd4d9fd24cbca4c221410f1a9684e00ec6dba6fb034e2bfae1c1b7",
"Created": "2023-07-06T13:12:54.8598131Z",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "172.17.0.0/16",
"Gateway": "172.17.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {},
"Options": {
"com.docker.network.bridge.default_bridge": "true",
"com.docker.network.bridge.enable_icc": "true",
"com.docker.network.bridge.enable_ip_masquerade": "true",
"com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
"com.docker.network.bridge.name": "docker0",
"com.docker.network.driver.mtu": "1500"
},
"Labels": {}
}
]
I have tried to run the container on another machine but without success.
Can someone tell me how to get out of my despair and where the error lies? Thanks in advance