
- Use Case
- Run a dockerized spring boot application which has dependency on PostgreSQL docker container
- Initialize the database with data while its getting created
- Mount an external volume so that the data does not get lost when container exits
- Solution:
- Step 1: Build a micro service using spring boot to be containerized
- Clone the following spring boot implementation
- The example service contains one end point to return all customer details
- Service uses Spring JPA and PostgreSQL database
- Snippet of application.properties file
spring.datasource.url=jdbc:postgresql://localhost:5432/northwind
spring.datasource.username=postgres
spring.datasource.password=changeme
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=update
- Data:
- Service uses a customer table and the initial scripts for table creation and data loading is available at
- https://github.com/rajeshsgr/postgres-docker-compose/tree/master/init
- These scripts will be leveraged to launch postgreSQL container
- Step 2: Dockerize the micro service
- Build the application to generate jar file
mvn clean install -DskipTests
- Create a dockerfile and add the below content (Already available in the git repo)
- Build the application to generate jar file
FROM openjdk:8-jdk-alpine
VOLUME /tmp
EXPOSE 8080
RUN mkdir -p /app/
RUN mkdir -p /app/logs/
ADD target/order-0.0.1-SNAPSHOT.jar /app/app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-Dspring.profiles.active=container", "-jar", "/app/app.jar"]
- Dockerfile has mainly instructions to add openjdk 8 image, expose port 8080 and add the generated jar file from previous step to the image
- To build an image with the name order-svc from the Dockerfile , run the below command
docker build -t order-svc .
- To view the images run
docker images

- Step 3: Define the Docker Compose yml file
- Now we have a docker image , which has a dependency on PostgreSQL database
- Next step is to define a docker-compose.yml file which will have a configuration to create a PostgreSQL database in a Docker container and connect it to the order-svc container of spring boot project to run as a multi-container application.
- Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration.
- Using Compose is basically a three-step process:
- Define your app’s environment with a Dockerfile .
- Define the services that make up your app in docker-compose.yml so they can be run together in an isolated environment.
- Run docker compose up and the Docker compose command starts and runs your entire app.
- In the project directory, create a
docker-compose.yml
file. It is available in the projects root directory in git .
- Now we have a docker image , which has a dependency on PostgreSQL database

- The above configurations define the services in the multi-container application.
- Now, you can run your multi-container application with Docker Compose.
- Step 4: Run the application
- Execute
docker compose up
- It will fire the application
- Execute

- Test the application with
curl http://localhost:8080/api/v1/customers
Thank you . Please share your comments / feedback
very good submit, i certainly love this web site, carry on it