forked from maxfield-allison/dnsweaver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile
More file actions
113 lines (91 loc) · 4.2 KB
/
Copy pathDockerfile
File metadata and controls
113 lines (91 loc) · 4.2 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# =============================================================================
# dnsweaver - Multi-Stage Dockerfile
# =============================================================================
#
# Image Strategy:
# :dev - Development/integration testing (develop branch)
# :edge - Bleeding edge from main branch
# :latest - Latest stable release (version tags)
# :vX.Y.Z - Specific version
# :sha-XXX - Specific commit for debugging
#
# Build commands:
# docker build -t dnsweaver:latest .
# docker build --platform linux/amd64,linux/arm64 -t dnsweaver:latest .
#
# Multi-arch support: amd64 + arm64
# =============================================================================
ARG GO_VERSION=1.25.11
ARG ALPINE_VERSION=3.23
# -----------------------------------------------------------------------------
# Stage 1: Go Builder (Multi-Arch Cross-Compilation)
# -----------------------------------------------------------------------------
FROM --platform=$BUILDPLATFORM golang:${GO_VERSION}-alpine AS builder
# Build arguments for multi-arch support
ARG TARGETPLATFORM
ARG TARGETOS
ARG TARGETARCH
ARG VERSION=dev
WORKDIR /build
# Install build dependencies
RUN apk add --no-cache git ca-certificates tzdata
# Copy go mod files first for layer caching
COPY go.mod go.sum* ./
RUN go mod download 2>/dev/null || true
# Copy source
COPY . .
# Build with cross-compilation for target architecture
# CGO_ENABLED=0 ensures pure Go build (no C dependencies)
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH:-amd64} go build \
-ldflags="-s -w -X main.Version=${VERSION} -X main.BuildDate=$(date -u +%Y-%m-%dT%H:%M:%SZ)" \
-o dnsweaver \
./cmd/dnsweaver
# Verify binary
RUN ls -la dnsweaver && file dnsweaver || true
# -----------------------------------------------------------------------------
# Stage 2: Minimal Runtime (Alpine)
# -----------------------------------------------------------------------------
FROM alpine:${ALPINE_VERSION}
# Labels
LABEL org.opencontainers.image.title="dnsweaver" \
org.opencontainers.image.description="Automatic DNS record management for Docker and Kubernetes workloads" \
org.opencontainers.image.source="https://gitlab.bluewillows.net/root/dnsweaver" \
org.opencontainers.image.vendor="bluewillows.net" \
org.opencontainers.image.base.name="alpine:3.23"
# Changing CACHE_BUST invalidates Docker layer cache for apk upgrade.
# CI passes --build-arg CACHE_BUST=$CI_PIPELINE_ID so every pipeline
# runs a fresh apk upgrade, even if the base image hash is unchanged.
ARG CACHE_BUST=dev
# Install runtime dependencies (no wget/curl — reduces attack surface)
# Upgrade base packages first to pick up security fixes
# su-exec: drops privileges from root to dnsweaver in the entrypoint after
# performing one-time docker socket GID detection.
RUN apk upgrade --no-cache && \
apk add --no-cache ca-certificates tzdata su-exec
# Create non-root user
RUN addgroup -g 1000 dnsweaver && \
adduser -u 1000 -G dnsweaver -s /bin/sh -D dnsweaver
# Copy binary from builder
COPY --from=builder /build/dnsweaver /usr/local/bin/dnsweaver
# Copy entrypoint script (handles Docker socket GID auto-detection)
COPY docker/entrypoint.sh /usr/local/bin/entrypoint.sh
# Ensure binary and entrypoint are executable
RUN chmod +x /usr/local/bin/dnsweaver /usr/local/bin/entrypoint.sh
# Default environment variables (can be overridden)
ENV DNSWEAVER_LOG_LEVEL="info" \
DNSWEAVER_LOG_FORMAT="json" \
DNSWEAVER_DRY_RUN="false" \
DNSWEAVER_HEALTH_PORT="8080"
# Health check (using busybox nc — no wget needed)
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
CMD ["/bin/sh", "-c", "echo -e 'GET /health HTTP/1.0\r\nHost: localhost\r\n\r\n' | nc localhost 8080 | grep -q '200 OK' || exit 1"]
# Note: container starts as root so the entrypoint can detect the docker
# socket GID and add the dnsweaver user to the matching group. The entrypoint
# then drops privileges via su-exec before invoking the binary, so the
# dnsweaver process always runs unprivileged.
#
# If you don't mount the docker socket (k8s-only, socket proxy), the entrypoint
# skips the GID logic entirely.
# Expose health port
EXPOSE 8080
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]