|
| 1 | +# Builder stage |
| 2 | +FROM golang:1.23-alpine AS builder |
| 3 | + |
| 4 | +# Install build dependencies |
| 5 | +RUN apk add --no-cache git gcc musl-dev |
| 6 | + |
| 7 | +# Set working directory for the build |
| 8 | +WORKDIR /build |
| 9 | + |
| 10 | +# Copy go.mod and go.sum files first for better caching |
| 11 | +COPY go.mod go.sum ./ |
| 12 | +RUN go mod download |
| 13 | + |
| 14 | +# Copy the source code |
| 15 | +COPY . . |
| 16 | + |
| 17 | +# Build the application |
| 18 | +# Using CGO_ENABLED=0 for static linking and setting GOARCH=amd64 for compatibility |
| 19 | +RUN CGO_ENABLED=1 \ |
| 20 | + go build -ldflags="-s -w" -o hashup . |
| 21 | + |
| 22 | +# Final stage |
| 23 | +FROM alpine:latest |
| 24 | + |
| 25 | +# Install runtime dependencies |
| 26 | +RUN apk add --no-cache ca-certificates tzdata sqlite |
| 27 | + |
| 28 | +# Create necessary directories |
| 29 | +RUN mkdir -p /etc/hashup /data/hashup |
| 30 | + |
| 31 | +# Add non-root user for running the service |
| 32 | +RUN adduser -D -h /home/hashup hashup |
| 33 | +RUN chown -R hashup:hashup /data/hashup |
| 34 | + |
| 35 | +# Set working directory |
| 36 | +WORKDIR /app |
| 37 | + |
| 38 | +# Copy the compiled binary from the builder stage |
| 39 | +COPY --from=builder /build/hashup /app/hashup |
| 40 | +RUN chmod +x /app/hashup |
| 41 | + |
| 42 | +# Copy default configurations |
| 43 | +COPY configs/nats.conf /etc/hashup/nats.conf |
| 44 | +COPY configs/hashup.toml /etc/hashup/config.toml |
| 45 | + |
| 46 | +# Expose NATS ports |
| 47 | +EXPOSE 4222 8222 |
| 48 | + |
| 49 | +# Volume for configuration and data |
| 50 | +VOLUME ["/etc/hashup", "/data/hashup"] |
| 51 | + |
| 52 | +# Switch to non-root user |
| 53 | +USER hashup |
| 54 | + |
| 55 | +ENTRYPOINT ["/app/hashup"] |
| 56 | +# Run hashup nats server |
| 57 | +CMD ["nats", "--config", "/etc/hashup/nats.conf", "--data-dir", "/data/hashup"] |
0 commit comments