-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathDockerfile
More file actions
60 lines (42 loc) · 1.41 KB
/
Dockerfile
File metadata and controls
60 lines (42 loc) · 1.41 KB
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
55
56
57
58
59
60
# Multi-stage build for minimal image size
FROM golang:1.25.6-alpine AS builder
# Install build dependencies
RUN apk add --no-cache git protobuf-dev bash
# Set working directory
WORKDIR /build
# Copy go mod files
COPY go.mod go.sum ./
# Download Go protobuf plugins
RUN go install google.golang.org/protobuf/cmd/protoc-gen-go@latest && \
go mod download
# Ensure protoc-gen-go is on PATH
ENV PATH="/root/go/bin:${PATH}"
# Copy source code
COPY . .
# Generate protobuf files from submodule
RUN bash generate_proto.sh
# Build the application
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o main -ldflags="-w -s" .
# Final stage - minimal runtime image
FROM alpine:3.20
# Install runtime dependencies (curl for health checks)
RUN apk add --no-cache ca-certificates
# Create non-root user for security
RUN addgroup -g 1000 metrolist && \
adduser -D -u 1000 -G metrolist metrolist
# Set working directory
WORKDIR /app
# Copy binary from builder
COPY --from=builder /build/main .
# Change ownership to non-root user
RUN chown -R metrolist:metrolist /app
# Switch to non-root user
USER metrolist
# Expose port (default 8080, can be overridden)
EXPOSE 8080
# Health check
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
CMD wget --no-verbose --tries=1 --spider http://localhost:8080/health || exit 1
# Run the application
# PORT can be overridden at runtime using -e PORT=<port>
CMD ["./main"]