# Stage 1: Build the Go application FROM golang:1.21-alpine AS builder # Set necessary environment variables ENV CGO_ENABLED=0 GOOS=linux GOARCH=amd64 WORKDIR /build # Copy only the module definition file first COPY go.mod ./ # DO NOT copy go.sum here initially. # Download dependencies and ensure go.sum is consistent # 'go mod tidy' synchronizes the go.mod and go.sum files with the source code imports. # It should be run after copying the source code. RUN go mod download # Copy the rest of the source code AFTER initial download COPY . . # Now run tidy to ensure go.mod and go.sum match the code RUN go mod tidy # Verify dependencies (optional but good practice) RUN go mod verify # Build the Go application statically linked # -ldflags="-w -s" reduces binary size by removing debug info RUN go build -ldflags="-w -s" -o /app/proxy-server . # Stage 2: Create the final minimal image FROM alpine:latest # Install ca-certificates for HTTPS calls and tzdata for timezone info RUN apk update && apk add --no-cache ca-certificates tzdata # Set the working directory WORKDIR /app # Copy the built binary from the builder stage COPY --from=builder /app/proxy-server /app/proxy-server # Expose the port the app runs on (using the default from config or ENV) # Defaulting to 7860 if not overridden by ENV PORT in runtime environment EXPOSE 7860 # Set the entrypoint command to run the binary # The application will read environment variables at runtime ENTRYPOINT ["/app/proxy-server"] # Optional: Add a non-root user for security (Uncomment if needed) # RUN addgroup -S appgroup && adduser -S appuser -G appgroup # USER appuser