Files
logcorrelator/Dockerfile
2026-02-27 15:31:46 +01:00

151 lines
4.9 KiB
Docker

# syntax=docker/dockerfile:1
# =============================================================================
# Builder stage - compile and test
# =============================================================================
FROM golang:1.21 AS builder
WORKDIR /build
# Install dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
git \
bc \
&& rm -rf /var/lib/apt/lists/*
# Copy go mod files
COPY go.mod ./
# Download dependencies
RUN go mod download || true
# Copy source code
COPY . .
# Run tests with coverage (fail if < 80%)
RUN --mount=type=cache,target=/root/.cache/go-build \
go test -race -coverprofile=coverage.txt -covermode=atomic ./... && \
echo "=== Coverage Report ===" && \
go tool cover -func=coverage.txt | grep total && \
TOTAL=$(go tool cover -func=coverage.txt | grep total | awk '{gsub(/%/, "", $3); print $3}') && \
echo "Total coverage: ${TOTAL}%" && \
if (( $(echo "$TOTAL < 80" | bc -l) )); then \
echo "ERROR: Coverage ${TOTAL}% is below 80% threshold"; \
exit 1; \
fi && \
echo "Coverage check passed!"
# Build binary
RUN --mount=type=cache,target=/root/.cache/go-build \
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \
-ldflags="-w -s" \
-o /usr/bin/logcorrelator \
./cmd/logcorrelator
# Create runtime root filesystem
RUN mkdir -p /tmp/runtime-root/var/log/logcorrelator /tmp/runtime-root/var/run/logcorrelator /tmp/runtime-root/etc/logcorrelator
# =============================================================================
# Runtime stage - minimal image for running the service
# =============================================================================
FROM gcr.io/distroless/base-debian12 AS runtime
# Copy binary from builder
COPY --from=builder /usr/bin/logcorrelator /usr/bin/logcorrelator
# Copy example config
COPY --from=builder /build/config.example.conf /etc/logcorrelator/logcorrelator.conf
# Create necessary directories in builder stage (distroless has no shell)
COPY --from=builder /tmp/runtime-root/var /var
COPY --from=builder /tmp/runtime-root/etc /etc
# Expose nothing (Unix sockets only)
# Health check not applicable for this service type
# Set entrypoint
ENTRYPOINT ["/usr/bin/logcorrelator"]
CMD ["-config", "/etc/logcorrelator/logcorrelator.conf"]
# =============================================================================
# RPM build stage - create .rpm package entirely in Docker
# =============================================================================
FROM ruby:3.2-bookworm AS rpm-builder
WORKDIR /build
# Install fpm and rpm tools
RUN apt-get update && apt-get install -y --no-install-recommends \
rpm \
&& rm -rf /var/lib/apt/lists/* \
&& gem install fpm -v 1.16.0
# Copy binary from builder stage
COPY --from=builder /usr/bin/logcorrelator /tmp/pkgroot/usr/bin/logcorrelator
# Copy config and systemd unit
COPY --from=builder /build/config.example.conf /tmp/pkgroot/etc/logcorrelator/logcorrelator.conf
COPY logcorrelator.service /tmp/pkgroot/etc/systemd/system/logcorrelator.service
# Create directory structure and set permissions
RUN mkdir -p /tmp/pkgroot/var/log/logcorrelator && \
mkdir -p /tmp/pkgroot/var/run/logcorrelator && \
chmod 755 /tmp/pkgroot/var/log/logcorrelator && \
chmod 755 /tmp/pkgroot/var/run/logcorrelator
# Build RPM
ARG VERSION=1.0.0
RUN fpm -s dir -t rpm \
-n logcorrelator \
-v ${VERSION} \
-C /tmp/pkgroot \
--prefix / \
--description "Log correlation service for HTTP and network events" \
--url "https://github.com/logcorrelator/logcorrelator" \
--license "MIT" \
--vendor "logcorrelator" \
-p /tmp/logcorrelator-${VERSION}.rpm \
usr/bin/logcorrelator \
etc/logcorrelator/logcorrelator.conf \
etc/systemd/system/logcorrelator.service \
var/log/logcorrelator \
var/run/logcorrelator
# =============================================================================
# Test stage - verify RPM on Rocky Linux
# =============================================================================
FROM rockylinux:8 AS rpm-test
# Install systemd (for testing service unit)
RUN dnf install -y systemd && \
dnf clean all
# Copy RPM from rpm-builder
COPY --from=rpm-builder /tmp/logcorrelator-*.rpm /tmp/
# Install the RPM
RUN rpm -ivh /tmp/logcorrelator-*.rpm || true
# Verify installation
RUN ls -la /usr/bin/logcorrelator && \
ls -la /etc/logcorrelator/ && \
ls -la /etc/systemd/system/logcorrelator.service
# =============================================================================
# Development stage - for local testing with hot reload
# =============================================================================
FROM golang:1.21 AS dev
WORKDIR /app
# Install air for hot reload (optional)
RUN go install github.com/air-verse/air@latest
COPY go.mod ./
RUN go mod download || true
COPY . .
# Default command: run with example config
CMD ["go", "run", "./cmd/logcorrelator", "-config", "config.example.conf"]