Run your Postgres Spring boot Application using Docker

How to Dockerize a Spring Boot App with PostgreSQL

ยท

1 min read

#Run this Application using Docker

  1. 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"]
    
  2. Build a Jar-file using :

    mvn clean package

  3. 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
  1. To run the application and create containers use this command :

    docker-compose up

  2. Check running application on http://localhost:8080/{mapped-name}

  3. Now you can open pgAdmin/PSQL and perform some CRUD operations.

  4. To stop the container : docker-compose down

ย