| 1 |
# Multi-stage build for ZephyrFS Coordinator |
| 2 |
FROM golang:1.21-alpine AS builder |
| 3 |
|
| 4 |
# Install build dependencies |
| 5 |
RUN apk add --no-cache git ca-certificates tzdata |
| 6 |
|
| 7 |
WORKDIR /app |
| 8 |
|
| 9 |
# Copy go mod files first for better caching |
| 10 |
COPY go.mod go.sum ./ |
| 11 |
RUN go mod download && go mod verify |
| 12 |
|
| 13 |
# Copy source code |
| 14 |
COPY . . |
| 15 |
|
| 16 |
# Build the application with optimizations |
| 17 |
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \ |
| 18 |
-a -installsuffix cgo \ |
| 19 |
-ldflags='-w -s -extldflags "-static"' \ |
| 20 |
-o coordinator cmd/coordinator/main.go |
| 21 |
|
| 22 |
# Runtime stage |
| 23 |
FROM alpine:3.18 |
| 24 |
|
| 25 |
# Install runtime dependencies |
| 26 |
RUN apk --no-cache add \ |
| 27 |
ca-certificates \ |
| 28 |
tzdata \ |
| 29 |
wget \ |
| 30 |
&& update-ca-certificates |
| 31 |
|
| 32 |
# Create non-root user for security |
| 33 |
RUN addgroup -g 1000 zephyrfs && \ |
| 34 |
adduser -D -s /bin/sh -u 1000 -G zephyrfs zephyrfs |
| 35 |
|
| 36 |
# Create necessary directories |
| 37 |
RUN mkdir -p /data /config /logs && \ |
| 38 |
chown -R zephyrfs:zephyrfs /data /config /logs |
| 39 |
|
| 40 |
WORKDIR /app |
| 41 |
|
| 42 |
# Copy binary from builder stage |
| 43 |
COPY --from=builder --chown=zephyrfs:zephyrfs /app/coordinator . |
| 44 |
|
| 45 |
# Create default configuration |
| 46 |
RUN echo 'database:\n type: "bbolt"\n path: "/data/coordinator.db"\ngrpc:\n port: 8080\nhttp:\n enabled: true\n port: 8090\nhealth:\n metrics_enabled: true\n metrics_port: 8091' > /config/config.yaml && \ |
| 47 |
chown zephyrfs:zephyrfs /config/config.yaml |
| 48 |
|
| 49 |
# Switch to non-root user |
| 50 |
USER zephyrfs |
| 51 |
|
| 52 |
# Expose ports |
| 53 |
EXPOSE 8080 8090 8091 |
| 54 |
|
| 55 |
# Health check |
| 56 |
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \ |
| 57 |
CMD wget --no-verbose --tries=1 --spider http://localhost:8091/health || exit 1 |
| 58 |
|
| 59 |
# Set default environment variables |
| 60 |
ENV CONFIG_PATH=/config/config.yaml |
| 61 |
ENV DATA_PATH=/data |
| 62 |
ENV LOG_LEVEL=info |
| 63 |
|
| 64 |
# Run the coordinator |
| 65 |
ENTRYPOINT ["./coordinator"] |
| 66 |
CMD ["-config", "/config/config.yaml", "-log-level", "info"] |