# Use multi-stage build to compile Go project from source and create a minimal runtime image.
FROM golang:1.22-bullseye AS builder
WORKDIR /src
# Cache modules
COPY go.mod go.sum ./
RUN go mod download
# Copy the rest of the source
COPY . .
# Build the binary for linux/amd64 with CGO disabled to create a static binary
ENV CGO_ENABLED=0
RUN GOOS=linux GOARCH=amd64 go build -ldflags "-s -w" -o /bin/aliyunpan
# Final runtime image
FROM debian:bookworm-slim
WORKDIR /app
# Copy the compiled binary from builder
COPY --from=builder /bin/aliyunpan /usr/local/bin/aliyunpan
# Prepare runtime directories
RUN mkdir -p /home/app/config /home/app/data
# Environment variables
ENV ALIYUNPAN_CONFIG_DIR=/home/app/config
ENV ALIYUNPAN_DOWNLOAD_DIR=/home/app/data
ENV TZ=Asia/Shanghai
# Ensure binary has executable permissions
RUN chmod 755 /usr/local/bin/aliyunpan
# Run aliyunpan by default
ENTRYPOINT ["/usr/local/bin/aliyunpan"]