File size: 1,640 Bytes
01d9631
 
cb5906c
01d9631
 
cb5906c
01d9631
cb5906c
01d9631
 
 
cb5906c
01d9631
 
 
 
cb5906c
01d9631
cb5906c
 
01d9631
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cb5906c
01d9631
 
 
cb5906c
01d9631
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# 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