# Spring Boot 백엔드 운영용 Dockerfile
FROM eclipse-temurin:21-jdk-alpine AS build
WORKDIR /app

# Gradle Wrapper 복사
COPY gradlew ./
COPY gradle ./gradle
RUN chmod +x gradlew

# 의존성 캐싱
COPY build.gradle settings.gradle ./
RUN ./gradlew dependencies --no-daemon || true

# 소스 복사 및 빌드
COPY src ./src
RUN ./gradlew bootJar --no-daemon

# Runtime image
FROM eclipse-temurin:21-jre-alpine AS runner
WORKDIR /app

RUN apk add --no-cache curl

# 비특권 사용자 생성
RUN addgroup -S appgroup && adduser -S -G appgroup appuser

# JAR 복사
COPY --from=build /app/build/libs/*.jar app.jar

# 디렉토리 생성
RUN mkdir -p logs uploads data && \
    chown -R appuser:appgroup /app && \
    chmod -R 755 /app

EXPOSE 8081
USER appuser
CMD ["java", "-jar", "app.jar"]
