I have a Dockerized Spring Boot 3 (Java21) application and I am connect to an existing Postgres database, running on DEV & PROD.
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-docker-compose</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
I have included the Spring Boot Docker Compose dependency, so that when Spring Boot starts up, it will automatically run Docker.
docker-compose.yml
version: '1.0'
services:
app:
build:
context: .
dockerfile: Dockerfile
This is required by the above dependency, but I just let it point to the Dockerfile.
Dockerfile
# Use the Amazon Corretto 21 base image
FROM amazoncorretto:21.0.3
# Create a volume for temporary files
VOLUME /tmp
# Arguments for the JAR file to be copied
ARG JAR_FILE=target/*.jar
#EXPOSE 8080
#EXPOSE 5432
# Copy the JAR file into the Docker image
COPY ${JAR_FILE} /app.jar
# Set the entry point for the container to run the application
ENTRYPOINT ["java","-jar","/app.jar"]
application.properties
spring.datasource.url=jdbc:postgresql://localhost:5432/my-datasource
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
application-prod.properties
spring.datasource.url=jdbc:postgresql://<my-prod-url>:5432/my-datasource
spring.datasource.username=postgres
spring.datasource.password=postgres
Logs on start:
Starting ExpiryServiceApplication using Java 21.0.3 with PID 190930
No active profile set, falling back to 1 default profile: "default"
Using Docker Compose file '.../expiry-service/docker-compose.yml'
There are already Docker Compose services running, skipping startup
Bootstrapping Spring Data JPA repositories in DEFAULT mode.
...
Starts with no errors
I do have a JPA entity object.
CountryEntity.java
@Entity
@Table(name = "country")
@NamedQueries( {
@NamedQuery(name="Country.findByName", query="SELECT o FROM CountryEntity o WHERE o.name = ?1")
})
public class CountryEntity {
private String country_code;
private String name;
private String printable_name;
private String iso3;
private String numcode;
private boolean regional;
When I call an endpoint to run the above query, it says that the entity doesn't exist.
Error
o.h.engine.jdbc.spi.SqlExceptionHelper : ERROR: relation "country" does not exist
However, the entity ("country
") does exist on the database (when connected via DBeaver SQL client).
jdbc:postgresql://localhost:5432/my-datasource
It is as if because this is a docker image, it is not connecting to the same database?
Question
How do i configure this to connect to the database and successfully query the table?
If I understood you correctly: you are trying to connect to the postgreSQL that is running on the localhost (of your local computer) and you're trying to connect from within the docker container. Since you've attempted to connect to the localhost inside of the docker container and not on your local computer, it fails.
If you want to connect to the localhost from docker container, you may use the way as expalined in:
i-want-to-connect-from-a-container-to-a-service-on-the-host
So, looking at this doc, you need to adjust your postgres url as :
jdbc:postgresql://host.docker.internal:5432/my-datasource
Or, you can also use:
jdbc:postgresql://<YOUR_MACHINE_IP>/my-datasource
Additionally, you may need to make postgreSQL is accessible and is accepting requests from docker container.
For that, you've to modify postgresql.conf file and listen to all address as :
listen_addresses = '*'
and edit pg_hba.conf to allow connection from docker network (find your docker IP and write it there).
host all all docker IP md5