fix: resolve CGO cross-compilation issues with multi-stage Docker builds

Problem:
- Enabling CGO_ENABLED=1 for SQLite support caused cross-compilation failures
- ARM64 assembly errors when building from amd64 host
- Cross-compilation with CGO requires architecture-specific toolchains

Solution:
- Converted all Dockerfiles to multi-stage builds
- Binaries now compile inside Docker using native platform builders
- Used --platform flag to build for target architecture natively
- Removed binary builds from .goreleaser.yaml (skip: true)
- Updated dockers_v2 to use buildx with multi-platform support

Changes:
- .goreleaser.yaml: Skip standalone builds, use Docker buildx
- Dockerfile.server: Multi-stage build with CGO
- Dockerfile.scanner: Multi-stage build with CGO
- Dockerfile.migrate: Multi-stage build with CGO

Benefits:
- No cross-compilation needed (each platform builds natively)
- Docker buildx handles multi-platform builds automatically
- SQLite support working with CGO enabled
- Cleaner separation between build and runtime environments
This commit is contained in:
2026-01-03 21:55:01 +00:00
parent f936dfa359
commit 311e4d13f6
4 changed files with 142 additions and 70 deletions
+38 -4
View File
@@ -1,7 +1,43 @@
# Application Engine - GoHoarder Server
# Multi-stage build to compile with CGO support
ARG TARGETOS=linux
ARG TARGETARCH=amd64
# Build stage
FROM --platform=$TARGETOS/$TARGETARCH golang:1.23-alpine AS builder
# Install build dependencies for CGO
RUN apk add --no-cache \
gcc \
g++ \
musl-dev \
sqlite-dev \
git
WORKDIR /build
# Copy go mod files
COPY go.mod go.sum ./
RUN go mod download
# Copy source code
COPY . .
# Build with CGO enabled
ARG TARGETOS
ARG TARGETARCH
ARG VERSION=dev
ARG GIT_COMMIT=unknown
ARG BUILD_TIME=unknown
RUN CGO_ENABLED=1 GOOS=$TARGETOS GOARCH=$TARGETARCH \
go build -ldflags="-s -w \
-X github.com/lukaszraczylo/gohoarder/internal/version.Version=${VERSION} \
-X github.com/lukaszraczylo/gohoarder/internal/version.GitCommit=${GIT_COMMIT} \
-X github.com/lukaszraczylo/gohoarder/internal/version.BuildTime=${BUILD_TIME}" \
-o gohoarder ./cmd/gohoarder
# Runtime stage
FROM alpine:latest
# Install runtime dependencies (including CGO/SQLite dependencies)
@@ -26,10 +62,8 @@ RUN mkdir -p /var/cache/gohoarder \
chmod -R 750 /var/cache/gohoarder \
/var/lib/gohoarder
# Copy binary (from platform-specific path)
ARG TARGETOS
ARG TARGETARCH
COPY ${TARGETOS}/${TARGETARCH}/gohoarder /usr/local/bin/gohoarder
# Copy binary from builder
COPY --from=builder /build/gohoarder /usr/local/bin/gohoarder
RUN chmod +x /usr/local/bin/gohoarder
# Copy example config