Run your Postgres Spring boot Application using Docker
How to Dockerize a Spring Boot App with PostgreSQL
Photo by AbsolutVision on Unsplash
#Run this Application using Docker
Create a Dockerfile for your project:
# Use a base image with Java FROM openjdk:17-jdk-slim # Set the working directory inside the container WORKDIR /app # Copy the built jar file from the target directory to the container COPY target/your-spring-boot-app.jar app.jar # Expose the port that your application runs on EXPOSE 8080 # Command to run the application ENTRYPOINT ["java", "-jar", "app.jar"]
Build a Jar-file using :
mvn clean package
Create an docker-compose yaml file for database integration :
version: '3.8'
services:
postgres:
image: postgres:15
container_name: psql-db
restart: always
environment:
POSTGRES_DB=$POSTGRESDB_DATABASE
POSTGRES_USER=$POSTGRESDB_USER
POSTGRES_PASSWORD=$POSTGRESDB_PASSWORD
ports:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data
To run the application and create containers use this command :
docker-compose up
Check running application on http://localhost:8080/{mapped-name}
Now you can open pgAdmin/PSQL and perform some CRUD operations.
To stop the container :
docker-compose down
ย