-1

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

Doncarlito87
  • 359
  • 1
  • 8
  • 25
  • 1
    `localhost` in Docker almost always refers to the current container. If the database is running outside a container on the same host, or in a different container, you'll need a different host name to refer to the database. See [From inside of a Docker container, how do I connect to the localhost of the machine?](https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach) if the database isn't in a container, or [Networking in Compose](https://docs.docker.com/compose/networking/) if it is. – David Maze Jul 06 '23 at 17:31

1 Answers1

1

For a container localhost is something unknown, either try with IP or hostname here:

 spring.datasource.url= jdbc:postgresql://<IP or NAME ie pgdb>:5432/postgres

Re run the app. If error message changes please make an update on the question.

Question: seems that you do not have a dockercompose file with a service for postgres ?

UPDATE:

build your current docker file and tag it (REMOVE the EXPOSE statement):

Docker file for app:

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"]

docker build -f -t app/app:latest .

Now create a docker compose as follows:

  version: "3"
  services:
    app:
      build: .
      container_name: <NAME PROVIDED ABOVE>
      environment:
        SPRING_DATASOURCE_URL: jdbc:postgresql://db:5432/postgres
      ports:
        - 8000:8080
      restart: always
      depends_on:
        - db
    db:
      image: postgres
      container_name: db
      environment:
        - POSTGRES_USER=<USER>
        - POSTGRES_PASSWORD=<PASS>
        - POSTGRES_DB=<DBNAME>
        - PGDATA=/var/lib/postgresql/data/pgdata
      ports:
        - 5000:5432
      volumes:
        - pgdata:/var/lib/postgresql/data
      restart: always
  volumes:
    pgdata:

Notice the name used on the datasource you can switch between localhost and service name (db)

jmvcollaborator
  • 2,141
  • 1
  • 6
  • 17