17. How to Run a Spring Boot Application on Docker Desktop with Docker Compose

This post guides you through running a Spring Boot CRUD application on Docker Desktop using a Dockerfile and a docker-compose.yml file. When you run the command docker compose up --build, the Spring Boot application image and the MySQL image are downloaded and installed on Docker Desktop, and the application starts running.

Steps to follow:

Search for Docker on your preferred search engine or go directly to the Docker download page

Select Docker Desktop based on your operating system and download it.

Inside your project directory, create a Dockerfile.

Copy and paste the Dockerfile content (provided below or from the GitHub repository) into your project and adjust the parameters according to your project configuration.

Create a docker-compose.yml file inside your project directory.

Copy and paste the docker-compose.yml content from the GitHub repository and customize it to fit your project settings.

Open a terminal in your project directory and run the command: docker compose up –build

Open your browser, type the application URL, and perform your CRUD operations.

You will see the MySQL and Spring Boot application images running on Docker Desktop.

The installed containers will show up as active in your Docker Desktop dashboard.

You can find the related GitHub repository branch here: book-details-docker

Dockerfile

# Build stage
FROM maven:3.9-eclipse-temurin-21 AS build
WORKDIR /app
COPY pom.xml .
RUN mvn dependency:go-offline  # Helps cache dependencies for faster builds
COPY . .
RUN mvn clean package -DskipTests
RUN ls -lh /app/target/

# Run stage
FROM eclipse-temurin:21-jre-jammy
WORKDIR /app
COPY --from=build /app/target/*.jar /app/book-details-0.0.1.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "book-details-0.0.1.jar"]

docker-compose.yml

version: '3.8'  # Corrected version

services:
  app:
    build: .
    ports:
      - "8080:8080"
    environment:
      - SPRING_DATASOURCE_URL=jdbc:mysql://db:3306/book_details?createDatabaseIfNotExist=true&useSSL=false&allowPublicKeyRetrieval=true
      - SPRING_DATASOURCE_USERNAME=root
      - SPRING_DATASOURCE_PASSWORD=root
    depends_on:
      db:
        condition: service_healthy
    networks:
      - book-network
    restart: always  # Added restart policy

  db:
    image: mysql:8.0.41
    ports:
      - "3307:3306"
    environment:
      - MYSQL_DATABASE=book_details
      - MYSQL_ROOT_PASSWORD=root  # Removed unnecessary MYSQL_USER
    volumes:
      - mysql-data:/var/lib/mysql
      #- ./init.sql:/docker-entrypoint-initdb.d/init.sql  # Ensure init.sql is valid
    networks:
      - book-network
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
      interval: 10s
      timeout: 5s
      retries: 3
    restart: always  # Added restart policy

networks:
  book-network:
    driver: bridge

volumes:
  mysql-data:

Leave a Reply

Your email address will not be published. Required fields are marked *