Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 15611

Spring Boot and Docker Compose cannot access Postgres

$
0
0

I am using Spring Boot 3.3.0 and Docker. I am using the spring-boot-docker-compose dependency so that Spring Boot starts the Docker image automatically when Spring Boot starts up.

pom.xml

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-docker-compose</artifactId><scope>runtime</scope><optional>true</optional></dependency>

This works as expected, i.e. my Docker image is loaded and the application can talk to the Postgres database.

Problem

When I use a SQL client (e.g. IntelliJ or DBeaver) and try connect to the database, it returns no data. The SQL client is connecting to a different database, when I need it to connect to the same database as the application.

I have read that I need to set the Docker network to:

--network host

Question

This may fix it, but with the Spring Boot dependency above, how do I do this? Or is there an alternative?

More info

docker-compose.yml

version: '3.5'services:  app:    build:      context: .      dockerfile: Dockerfile    ports:      - "8081:8080"    environment:      SPRING_PROFILES_ACTIVE: ACTIVE_PROFILE  db:    image: postgres    ports:      - 5432:5432    volumes:      - data:/var/lib/postgresql    environment:      - POSTGRES_PASSWORD=postgres      - POSTGRES_USER=postgres      - POSTGRES_DB=expiry_service    restart: on-failure    container_name: db

Dockerfile

# Use the Amazon Corretto 21 base imageFROM amazoncorretto:21.0.3# Create a volume for temporary filesVOLUME /tmp# Arguments for the JAR file to be copiedARG JAR_FILE=target/*.jarEXPOSE 8080# Copy the JAR file into the Docker imageCOPY ${JAR_FILE} /app.jar# Set the entry point for the container to run the applicationENTRYPOINT ["java","-jar","/app.jar"]

application.properties (that connects the application successfully and retrieves data)

spring.datasource.url=jdbc:postgresql://localhost:5432/expiry_servicespring.datasource.username=postgresspring.datasource.password=postgres

Localhost SQL Client (DBeaver) DB connection that cannot see the data

jdbc:postgresql://localhost:5432/expiry_serviceUsername: postgres, password: postgres

Here is my container:

enter image description here


Viewing all articles
Browse latest Browse all 15611

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>