I'am trying to use docker-compose with a springboot app(uses JPA) and a postgres database, I am kinda new using docker and I don't know what I am doing wrong.
Problem
When I build the springboot app, if I change
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres(code that works) to host it in postgres container, spring.datasource.url=jdbc:postgresql://postgres:5432/postgres.
It throws me a PSQLException caused by: java.net.UnknownHostException: postgres.(More details below)
application.properties
server.port = 8080
## default connection pool
spring.datasource.hikari.connectionTimeout=20000
spring.datasource.hikari.maximumPoolSize=5
## PostgreSQL
spring.datasource.url=jdbc:postgresql://postgres:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.jpa.hibernate.ddl-auto=create
Exception
2020-06-22 19:07:10.597 INFO 16088 --- [ main] c.a.a.AnnotationToolApplicationTests : Starting AnnotationToolApplicationTests on DESKTOP-P1E07PA with PID 16088 (started by Pedro in C:\Users\Pedro\git\tfg\annotation-tool\annotation-tool)
2020-06-22 19:07:10.598 INFO 16088 --- [ main] c.a.a.AnnotationToolApplicationTests : No active profile set, falling back to default profiles: default
2020-06-22 19:07:11.584 INFO 16088 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2020-06-22 19:07:11.719 INFO 16088 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 120ms. Found 3 JPA repository interfaces.
2020-06-22 19:07:12.235 INFO 16088 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2020-06-22 19:07:12.572 INFO 16088 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2020-06-22 19:07:12.746 INFO 16088 --- [ main] org.hibernate.Version : HHH000412: Hibernate Core {5.4.10.Final}
2020-06-22 19:07:13.002 INFO 16088 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
2020-06-22 19:07:13.434 INFO 16088 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2020-06-22 19:07:16.873 ERROR 16088 --- [ main] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Exception during pool initialization.
org.postgresql.util.PSQLException: The connection attempt failed.
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:292) ~[postgresql-42.2.9.jar:42.2.9]
...
at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:126) ~[surefire-booter-2.22.2.jar:2.22.2]
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:418) ~[surefire-booter-2.22.2.jar:2.22.2]
Caused by: java.net.UnknownHostException: postgres
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184) ~[na:1.8.0_222]
docker-compose.yml
version: '3'
services:
# SpringBoot App
postgres:
image: "postgres:9.6-alpine"
ports:
- 5432:5432
volumes:
- apiDB:/var/lib/postgresql/data
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
api:
build: ./annotation-tool
ports:
- 8080:8080
depends_on:
- postgres
client:
container_name: client-container
build: ./client
ports:
- 8081:8081
volumes:
apiDB:
Api Dockerfile
FROM maven:3.6.1-jdk-8-slim AS build
RUN mkdir -p /workspace
WORKDIR /workspace
COPY pom.xml /workspace
COPY src /workspace/src
RUN mvn -f pom.xml clean package
FROM openjdk:8
COPY --from=build /workspace/target/*.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","app.jar"]
Things that I think that happened/ tried
Maybe the DNS is not working, but I don't know how I can solve it
I tried differents docker-compose and Dockerfiles and it doesn't work
If I run the docker-compose the postgres container is created succesfully
I tried changing the url host to postgres-backend as my container was named that way
I tried by using links between my components.
postgres-backend, can you try changing the url to include that instead?spring.datasource.url=jdbc:postgresql://postgres-backend:5432/postgres, same errorlinks:andcontainer_name:options; they probably won't make a difference but might confuse things, and they're not necessary. At what point in your application's execution are you trying to connect to the database? (Do you get this exception in response to an HTTP request? Are you trying to run migrations in a Dockerfile?)