Docker Compose for Spring Boot application with PostgreSQL
In this blog post you will learn how to configure Spring Boot application with PostgreSQL for running with Docker Compose.
This blog post covers:
- Spring Boot application
Dockerfileconfiguration with clean separation between dependencies and resources Docker Composeconfiguration for running the application with PostgreSQL
Prerequisites
- Docker
- Java 13
- Terminal
- httpie (or curl)
Application
- Generate the Maven based Spring Boot application with
Spring Web,Spring Data JPA,Spring Data REST,PostgreSQL JDBC Driverdependencies.
The source code for this article can be found on Github: https://github.com/kolorobot/spring-boot-tc
Dockerfile
- Create
Dockerfile - Base Docker image uses
AlpineLinux:
1 | FROM openjdk:13-alpine |
- Do not run the application as
root:
1 2 | RUN addgroup -S spring && adduser -S spring -G springUSER spring:spring |
- Do not deploy the fat-jar into the container, but rather split dependencies and application classes and resources into separate layers:
1 2 3 4 | ARG DEPENDENCY=target/dependencyCOPY ${DEPENDENCY}/BOOT-INF/lib /app/libCOPY ${DEPENDENCY}/META-INF /app/META-INFCOPY ${DEPENDENCY}/BOOT-INF/classes /app |
Note: ARG can be used to adjust the directory, in case you you have Gradle based project: docker build --build-arg DEPENDENCY=build/dependency -t spring-boot-tc .
- Run the application inside the containers by pointing the main class and the libs in the
javacommand:
1 | ENTRYPOINT ["java","-cp","app:app/lib/*","pl.codeleak.samples.springboot.tc.SpringBootTestcontainersApplication"] |
The complete Dockerfile:
1 2 3 4 5 6 7 8 | FROM openjdk:13-alpineRUN addgroup -S spring && adduser -S spring -G springUSER spring:springARG DEPENDENCY=target/dependencyCOPY ${DEPENDENCY}/BOOT-INF/lib /app/libCOPY ${DEPENDENCY}/META-INF /app/META-INFCOPY ${DEPENDENCY}/BOOT-INF/classes /appENTRYPOINT ["java","-cp","app:app/lib/*","pl.codeleak.samples.springboot.tc.SpringBootTestcontainersApplication"] |
New to Docker? Docker explained in 12 minutes: https://www.youtube.com/watch?v=YFl2mCHdv24
Docker Compose
- Create
docker-compose.yml - We will have two
services:dbfor PostgreSQL database andappfor the applicationdbservice will use thepostgresimage from a public repository, it will expose port5432to the host and it will pass the environment propertiesPOSTGRES_*to the container to setup the database name, user and password.appservice will use the local build we created earlier, it will expose port9000to the host and it will pass the environment properties that will override the datasource configuration of the application (application.properties). Theappservice will depend ondbservice. The datasource URL uses thedbas hostname which reflects the name of thedbservice.
The complete docker-compose.yml:
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 | version: '3'services: db: image: "postgres" ports: - "5432:5432" environment: POSTGRES_DB: tc POSTGRES_USER: tc POSTGRES_PASSWORD: tc app: build: . ports: - "9000:8080" environment: SPRING_DATASOURCE_URL: jdbc:postgresql://db/tc SPRING_DATASOURCE_USERNAME: tc SPRING_DATASOURCE_PASSWORD: tc depends_on: - db |
New to Docker Compose? Docker Compose explained in 12 min: https://www.youtube.com/watch?v=Qw9zlE3t8Ko
Running the application
- Package the application
$ ./mvnw clean package
To skip the tests use: -DskipTests=true
- Extract libraries from
fat-jar
$ mkdir -p target/dependency && (cd target/dependency; jar -xf ../*.jar)
- Run with
docker-compose
$ docker-compose build && docker-compose up
- Verify the application is running and responding to requests
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | $ http get :9000/ownersHTTP/1.1 200Connection: keep-alive{ "_embedded": { "owners": [] }, "_links": { "profile": { }, "self": { "templated": true } }, "page": { "number": 0, "size": 20, "totalElements": 0, "totalPages": 0 }} |
Source code
The source code for this article can be found on Github: https://github.com/kolorobot/spring-boot-tc
References
- https://spring.io/guides/gs/spring-boot-docker/
- https://openliberty.io/blog/2018/06/29/optimizing-spring-boot-apps-for-docker.html
See also
Published on Java Code Geeks with permission by Rafal Borowiec, partner at our JCG program. See the original article here: Docker Compose for Spring Boot application with PostgreSQL Opinions expressed by Java Code Geeks contributors are their own. |





Hi…
I’m Elena gillbert.Spring Boot, Spring Security, JPA, and Docker Prerequisites: Docker; JDK 1.8; Maven 3.* Install and run the project. download/clone the project; Build the project using following maven command from project root folder where pom.xml file place.