# Build stage
FROM golang:1.26-alpine AS builder

WORKDIR /app

# Copy dependency files first to leverage Docker cache
COPY go.mod  ./

# Copy source code
COPY . .

# Build the application
RUN go build -o server .


# Runtime stage
FROM alpine:3.20

# Create a non-root user
RUN adduser -D appuser

WORKDIR /app

# Copy the compiled binary
COPY --from=builder /app/server .

# Run as non-root user
USER appuser

# Document the application port
EXPOSE 8080

# Start the application
CMD ["./server"]