-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathDockerfile
More file actions
66 lines (49 loc) · 1.84 KB
/
Copy pathDockerfile
File metadata and controls
66 lines (49 loc) · 1.84 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
65
66
# Multi-stage Dockerfile for production SPQR router image
# This creates a minimal image with only the router binary and runtime dependencies
# Stage 1: Build the router binary
FROM golang:1.26-alpine AS builder
# Install build dependencies
RUN apk add --no-cache git make gcc musl-dev
WORKDIR /build
# Copy go mod files first for better layer caching
COPY go.mod go.sum ./
RUN go mod download
# Copy the entire source code
COPY . .
# Build the router binary with version info
ARG GIT_REVISION=unknown
ARG SPQR_VERSION=unknown
RUN go build -ldflags "-X github.com/pg-sharding/spqr/pkg.GitRevision=${GIT_REVISION} -X github.com/pg-sharding/spqr/pkg.SpqrVersion=${SPQR_VERSION} -w -s" -o spqr-router ./cmd/router
# Stage 2: Create minimal runtime image
FROM alpine:3.19
# Install runtime dependencies
RUN apk add --no-cache \
ca-certificates \
tzdata \
postgresql-client \
&& rm -rf /var/cache/apk/*
# Create non-root user for running the router
RUN addgroup -g 1000 spqr && \
adduser -D -u 1000 -G spqr spqr
# Create directories for configuration and logs
RUN mkdir -p /etc/spqr /var/log/spqr /var/run/spqr && \
chown -R spqr:spqr /etc/spqr /var/log/spqr /var/run/spqr
# Copy the router binary from builder
COPY --from=builder /build/spqr-router /usr/local/bin/spqr-router
RUN chmod +x /usr/local/bin/spqr-router
# Copy default configuration
COPY examples/router.yaml /etc/spqr/router.yaml.example
WORKDIR /var/run/spqr
# Switch to non-root user
USER spqr
# Expose default ports
# 6432 - router port
# 7432 - admin console port
# 7000 - gRPC API port
EXPOSE 6432 7432 7000
# Health check
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
CMD psql -h localhost -p 7432 -U demo -d demo -c "SHOW shards;" || exit 1
# Default command
ENTRYPOINT ["/usr/local/bin/spqr-router"]
CMD ["run", "--config", "/etc/spqr/router.yaml"]