Dockarize Spring Boot Application with docker compose.

Step 1: Create a Spring Boot Application If you don't already have a Spring Boot project, you can create one using Spring Initializer or your preferred method.

Step 2: Configure Your Spring Boot Application Make sure your Spring Boot application is properly configured to connect to a PostgreSQL database. This usually involves setting up your application.properties or application.yml with the necessary database connection information.

Here's an example application.properties:

spring.datasource.url=jdbc:postgresql://postgres:5432/mydatabase
spring.datasource.username=myusername
spring.datasource.password=mypassword
spring.jpa.hibernate.ddl-auto=update

In this example, we assume that the hostname of the PostgreSQL container in Docker Compose will be postgres, and the database name is mydatabase.

Step 3: Create a Dockerfile for Your Application Create a Dockerfile in the root of your Spring Boot project. This file will be used to build a Docker image for your application.

In this example, we assume that the hostname of the PostgreSQL container in Docker Compose will be postgres, and the database name is mydatabase.

Step 3: Create a Dockerfile for Your Application

Create a Dockerfile in the root of your Spring Boot project. This file will be used to build a Docker image for your application.

# Stage 1: Build the application
# Use a base image with Java and Maven pre-installed
FROM maven:3.8.4-openjdk-17 AS build

# Set the working directory in the container
WORKDIR /app

# Copy the Maven project file (pom.xml) and download dependencies
COPY pom.xml .
RUN mvn dependency:go-offline -B

# Copy the source code into the container
COPY src ./src

# Build the JAR file with Maven (skip tests)
RUN mvn package -DskipTests

# Stage 2: Run the application
# Use a lightweight base image for the final image
FROM openjdk:17-alpine

# Copy the JAR file from the previous stage to the final image
COPY --from=build /app/target/your-app-name.jar /app.jar

# Expose the port your Spring Boot app is listening on
EXPOSE 8080

# Command to run the Spring Boot application
CMD ["java", "-jar", "/app.jar"]

Make sure to replace your-app-name.jar with the actual name of your JAR file.

Step 4: Create a Docker Compose File

Create a docker-compose.yml file in the root of your project to define your application and PostgreSQL service.

version: '3'
services:
  postgres:
    image: postgres:latest
    environment:
      POSTGRES_DB: mydatabase
      POSTGRES_USER: myusername
      POSTGRES_PASSWORD: mypassword
    ports:
      - "5432:5432"
    volumes:
      - postgres-data:/var/lib/postgresql/data

  your-app:
    build:
      context: .
      dockerfile: Dockerfile
    image: 'your-app-image-name'
    ports:
      - "8080:8080"
    depends_on:
      - postgres
    environment:
      DOCKER_DEFAULT_PLATFORM: linux/amd64
      SPRING_DATASOURCE_URL: jdbc:postgresql://postgres:5432/mydatabase
      SPRING_DATASOURCE_USERNAME: myusername
      SPRING_DATASOURCE_PASSWORD: mypassword

volumes:
  postgres-data:

This Docker Compose file defines two services: postgres for the PostgreSQL database and your-app for your Spring Boot application. It also sets up environment variables for the database connection.

Step 5: Build and Run Your Docker Containers

Now, open a terminal and navigate to your project directory. Run the following commands:

# Build the Docker images
docker-compose build

# Start the Docker containers
docker-compose up

Your Spring Boot application and PostgreSQL database will start within Docker containers. You can also run docker-compose up --build instead of two commands

Or if you want to build your image for docker hub only, then follow the command

docker buildx build --platform linux/amd64 -t yourdockerhubusername/imageName:latest .
docker push yourdockerhubusername/imageName:latest

Step 6: Access Your Application

Once the containers are up and running, you can access your Spring Boot application at http://localhost:8080. The PostgreSQL database will be available at localhost:5432.

Step 7: Push Docker Image to Docker Hub

If you want to push your Docker image to Docker Hub, you can follow these steps:

  • Log in to Docker Hub using docker login.

  • Tag your local Docker image with your Docker Hub username and repository name:

docker tag your-local-image-name:latest yourdockerhubusername/your-app-name:latest

Push the Docker image to Docker Hub:

docker push yourdockerhubusername/your-app-name:latest

Now, your Docker image is available on Docker Hub and can be pulled by others.