Repository navigation
Expand file tree
/
Copy pathDockerfile
More file actions
64 lines (45 loc) · 1.6 KB
/
Copy pathDockerfile
File metadata and controls
64 lines (45 loc) · 1.6 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
61
62
63
64
# 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@v1.36.11 && \
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 scripts/generate_proto.sh
# Build the application
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o metroserver -ldflags="-w -s" ./cmd/metroserver
# 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/metroserver .
# Change ownership to non-root user
RUN mkdir /app/data && chown -R metrolist:metrolist /app
# Switch to non-root user
USER metrolist
# Keep rooms and User-Agent counts on the data volume
ENV DATABASE_FILE=/app/data/metroserver.db
VOLUME /app/data
# 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:${PORT:-8080}/health" || exit 1
# Run the application
# PORT can be overridden at runtime using -e PORT=<port>
CMD ["./metroserver"]