Add Docker support with auto database initialization
This commit is contained in:
13
Dockerfile
Normal file
13
Dockerfile
Normal 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
36
docker-compose.yml
Normal 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:
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,9 +3,9 @@ spring:
|
|||||||
name: petshop-backend
|
name: petshop-backend
|
||||||
|
|
||||||
datasource:
|
datasource:
|
||||||
url: ${DB_URL:jdbc:mysql://localhost:3306/petshop?createDatabaseIfNotExist=true}
|
url: ${SPRING_DATASOURCE_URL:jdbc:mysql://localhost:3306/Petstoredb?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC}
|
||||||
username: ${DB_USERNAME:root}
|
username: ${SPRING_DATASOURCE_USERNAME:petshop}
|
||||||
password: ${DB_PASSWORD:password}
|
password: ${SPRING_DATASOURCE_PASSWORD:petshop}
|
||||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||||
|
|
||||||
jpa:
|
jpa:
|
||||||
@@ -29,7 +29,7 @@ springdoc:
|
|||||||
path: /swagger-ui
|
path: /swagger-ui
|
||||||
|
|
||||||
jwt:
|
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}
|
expiration: ${JWT_EXPIRATION:86400000}
|
||||||
|
|
||||||
logging:
|
logging:
|
||||||
|
|||||||
Reference in New Issue
Block a user