Add Docker support with auto database initialization

This commit is contained in:
2026-03-04 21:46:29 -07:00
parent aeb8002b2b
commit 5b4797fdcc
4 changed files with 93 additions and 4 deletions

13
Dockerfile Normal file
View File

@@ -0,0 +1,13 @@
# Build
FROM maven:3.9-eclipse-temurin-17 AS build
WORKDIR /app
COPY pom.xml .
COPY src ./src
RUN mvn -q -DskipTests package
# Run
FROM eclipse-temurin:17-jre
WORKDIR /app
COPY --from=build /app/target/*.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","app.jar"]

36
docker-compose.yml Normal file
View File

@@ -0,0 +1,36 @@
services:
db:
image: mysql:8.0
container_name: petshop-db
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: Petstoredb
MYSQL_USER: petshop
MYSQL_PASSWORD: petshop
ports:
- "3306:3306"
volumes:
- db_data:/var/lib/mysql
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-h", "127.0.0.1", "-uroot", "-proot"]
interval: 5s
timeout: 5s
retries: 30
api:
build: .
container_name: petshop-api
environment:
SPRING_DATASOURCE_URL: jdbc:mysql://db:3306/Petstoredb?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC
SPRING_DATASOURCE_USERNAME: petshop
SPRING_DATASOURCE_PASSWORD: petshop
# Change this in real use
JWT_SECRET: change_me_please
ports:
- "8080:8080"
depends_on:
db:
condition: service_healthy
volumes:
db_data:

View File

@@ -0,0 +1,40 @@
package com.petshop.backend.config;
import com.petshop.backend.entity.User;
import com.petshop.backend.repository.UserRepository;
import org.springframework.boot.CommandLineRunner;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
@Component
public class DataInitializer implements CommandLineRunner {
private final UserRepository userRepository;
private final PasswordEncoder passwordEncoder;
@Override
public void run(String... args) {
if (userRepository.findByUsername("admin").isEmpty()) {
User admin = new User();
admin.setUsername("admin");
admin.setPassword(passwordEncoder.encode("admin123"));
admin.setFullName("Admin User");
admin.setEmail("admin@petshop.com");
admin.setRole(User.Role.ADMIN);
admin.setActive(true);
userRepository.save(admin);
}
if (userRepository.findByUsername("staff").isEmpty()) {
User staff = new User();
staff.setUsername("staff");
staff.setPassword(passwordEncoder.encode("staff123"));
staff.setFullName("Staff User");
staff.setEmail("staff@petshop.com");
staff.setRole(User.Role.STAFF);
staff.setActive(true);
userRepository.save(staff);
}
}
}

View File

@@ -3,9 +3,9 @@ spring:
name: petshop-backend
datasource:
url: ${DB_URL:jdbc:mysql://localhost:3306/petshop?createDatabaseIfNotExist=true}
username: ${DB_USERNAME:root}
password: ${DB_PASSWORD:password}
url: ${SPRING_DATASOURCE_URL:jdbc:mysql://localhost:3306/Petstoredb?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC}
username: ${SPRING_DATASOURCE_USERNAME:petshop}
password: ${SPRING_DATASOURCE_PASSWORD:petshop}
driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
@@ -29,7 +29,7 @@ springdoc:
path: /swagger-ui
jwt:
secret: ${JWT_SECRET:your-256-bit-secret-key-change-this-in-production-min-32-chars}
secret: ${JWT_SECRET:change_me_please_make_this_at_least_32_characters_long_for_security}
expiration: ${JWT_EXPIRATION:86400000}
logging: