|
| 1 | +# ============================================================================= |
| 2 | +# technitium-companion - Multi-Stage Dockerfile |
| 3 | +# ============================================================================= |
| 4 | +# |
| 5 | +# Image Strategy: |
| 6 | +# :dev - Development/integration testing (develop branch) |
| 7 | +# :edge - Bleeding edge from main branch |
| 8 | +# :latest - Latest stable release (version tags) |
| 9 | +# :vX.Y.Z - Specific version |
| 10 | +# :sha-XXX - Specific commit for debugging |
| 11 | +# |
| 12 | +# Build commands: |
| 13 | +# docker build -t technitium-companion:latest . |
| 14 | +# docker build --platform linux/amd64,linux/arm64 -t technitium-companion:latest . |
| 15 | +# |
| 16 | +# Multi-arch support: amd64 + arm64 |
| 17 | +# ============================================================================= |
| 18 | + |
| 19 | +ARG GO_VERSION=1.24 |
| 20 | +ARG ALPINE_VERSION=3.20 |
| 21 | + |
| 22 | +# ----------------------------------------------------------------------------- |
| 23 | +# Stage 1: Go Builder (Multi-Arch Cross-Compilation) |
| 24 | +# ----------------------------------------------------------------------------- |
| 25 | +FROM --platform=$BUILDPLATFORM golang:${GO_VERSION}-alpine AS builder |
| 26 | + |
| 27 | +# Build arguments for multi-arch support |
| 28 | +ARG TARGETPLATFORM |
| 29 | +ARG TARGETOS |
| 30 | +ARG TARGETARCH |
| 31 | +ARG VERSION=dev |
| 32 | + |
| 33 | +WORKDIR /build |
| 34 | + |
| 35 | +# Install build dependencies |
| 36 | +RUN apk add --no-cache git ca-certificates tzdata |
| 37 | + |
| 38 | +# Copy go mod files first for layer caching |
| 39 | +COPY go.mod go.sum ./ |
| 40 | +RUN go mod download |
| 41 | + |
| 42 | +# Copy source |
| 43 | +COPY . . |
| 44 | + |
| 45 | +# Build with cross-compilation for target architecture |
| 46 | +# CGO_ENABLED=0 ensures pure Go build (no C dependencies) |
| 47 | +RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH:-amd64} go build \ |
| 48 | + -ldflags="-s -w -X main.Version=${VERSION} -X main.BuildDate=$(date -u +%Y-%m-%dT%H:%M:%SZ)" \ |
| 49 | + -o technitium-companion \ |
| 50 | + ./cmd/technitium-companion |
| 51 | + |
| 52 | +# Verify binary |
| 53 | +RUN ls -la technitium-companion && file technitium-companion || true |
| 54 | + |
| 55 | +# ----------------------------------------------------------------------------- |
| 56 | +# Stage 2: Runtime (Alpine) |
| 57 | +# ----------------------------------------------------------------------------- |
| 58 | +FROM alpine:${ALPINE_VERSION} |
| 59 | + |
| 60 | +# Labels |
| 61 | +LABEL org.opencontainers.image.title="technitium-companion" \ |
| 62 | + org.opencontainers.image.description="Automatic DNS record management for Docker containers via Technitium" \ |
| 63 | + org.opencontainers.image.source="https://gitlab.bluewillows.net/root/technitium-companion" \ |
| 64 | + org.opencontainers.image.vendor="bluewillows.net" |
| 65 | + |
| 66 | +# Install runtime dependencies |
| 67 | +RUN apk add --no-cache ca-certificates tzdata wget |
| 68 | + |
| 69 | +# Create non-root user |
| 70 | +RUN addgroup -g 1000 companion && \ |
| 71 | + adduser -u 1000 -G companion -s /bin/sh -D companion |
| 72 | + |
| 73 | +# Copy binary from builder |
| 74 | +COPY --from=builder /build/technitium-companion /usr/local/bin/technitium-companion |
| 75 | + |
| 76 | +# Ensure binary is executable |
| 77 | +RUN chmod +x /usr/local/bin/technitium-companion |
| 78 | + |
| 79 | +# Default environment variables (can be overridden) |
| 80 | +ENV TECHNITIUM_URL="" \ |
| 81 | + TECHNITIUM_TOKEN="" \ |
| 82 | + TECHNITIUM_ZONE="" \ |
| 83 | + TARGET_IP="" \ |
| 84 | + LOG_LEVEL="info" \ |
| 85 | + HEALTH_PORT="8080" |
| 86 | + |
| 87 | +# Health check |
| 88 | +HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \ |
| 89 | + CMD wget -qO- http://localhost:8080/health || exit 1 |
| 90 | + |
| 91 | +# Run as non-root user |
| 92 | +# Note: When mounting Docker socket, ensure socket has appropriate permissions |
| 93 | +# or run as root if needed for Docker API access |
| 94 | +USER companion |
| 95 | + |
| 96 | +# Expose health port |
| 97 | +EXPOSE 8080 |
| 98 | + |
| 99 | +ENTRYPOINT ["/usr/local/bin/technitium-companion"] |
0 commit comments