1- # Use an official Python runtime as a parent image
2- FROM python:3.9-slim
1+ # Start with a small base image
2+ FROM golang:1.24-alpine AS builder
33
4- # Set the working directory in the container
4+ # Install git and build essentials
5+ RUN apk add --no-cache git gcc musl-dev
6+
7+ # Set working directory
58WORKDIR /app
69
7- COPY requirements.txt /app/requirements.txt
8- # Install any needed packages specified in requirements.txt
9- RUN pip install --no-cache-dir -r requirements.txt
10+ # Copy go.mod and go.sum first to leverage Docker cache
11+ COPY go.mod go.sum* ./
12+
13+ # Download dependencies (if go.sum exists)
14+ RUN if [ -f go.sum ]; then go mod download; else go mod tidy; fi
15+
16+ # Copy source code
17+ COPY *.go ./
18+
19+ # Build the application with optimizations
20+ RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags="-s -w" -o collapse_queries .
21+
22+ # Use a minimal alpine image for the final container
23+ FROM alpine:3.18
24+
25+ # Add CA certificates for HTTPS
26+ RUN apk --no-cache add ca-certificates tzdata
27+
28+ # Set working directory
29+ WORKDIR /app
30+
31+ # Copy the binary from the builder stage
32+ COPY --from=builder /app/collapse_queries .
33+
34+ # Copy the .env file if it exists (optional, can be mounted)
35+ COPY .env* ./
36+
37+ # Create a non-root user and set permissions
38+ RUN adduser -D -H -h /app appuser && \
39+ chown -R appuser:appuser /app
1040
11- # Copy the current directory contents into the container at /app
12- COPY collapse_queries.py /app/collapse_queries.py
41+ USER appuser
1342
14- # Run script.py when the container launches
15- CMD ["python" , " collapse_queries.py " ]
43+ # Command to run the application
44+ ENTRYPOINT ["/app/ collapse_queries" ]
0 commit comments