|
| 1 | +# Multi-stage Dockerfile for HTTP client with OpenTelemetry compile-time instrumentation |
| 2 | +# Stage 1: Build the otel tool |
| 3 | +FROM golang:1.24-alpine AS otel-builder |
| 4 | + |
| 5 | +WORKDIR /build |
| 6 | + |
| 7 | +# Install build dependencies |
| 8 | +RUN apk add --no-cache git make gcc musl-dev |
| 9 | + |
| 10 | +# Copy the entire project to build the otel tool |
| 11 | +COPY go.mod go.sum ./ |
| 12 | +COPY . . |
| 13 | + |
| 14 | +# Build the otel tool |
| 15 | +RUN go build -o /otel ./tool/cmd |
| 16 | + |
| 17 | +# Stage 2: Build the HTTP client with instrumentation |
| 18 | +FROM golang:1.24-alpine AS app-builder |
| 19 | + |
| 20 | +WORKDIR /app |
| 21 | + |
| 22 | +# Install git for go mod download |
| 23 | +RUN apk add --no-cache git |
| 24 | + |
| 25 | +# Copy the otel tool from previous stage |
| 26 | +COPY --from=otel-builder /otel /usr/local/bin/otel |
| 27 | + |
| 28 | +# Copy HTTP client source |
| 29 | +COPY demo/http/client /app |
| 30 | + |
| 31 | +# Download dependencies |
| 32 | +RUN go mod download |
| 33 | + |
| 34 | +# Build the app with otel instrumentation |
| 35 | +RUN otel go build -o client . |
| 36 | + |
| 37 | +# Stage 3: Runtime image |
| 38 | +FROM alpine:3.21 |
| 39 | + |
| 40 | +# Install ca-certificates for TLS and timezone data |
| 41 | +RUN apk --no-cache add ca-certificates tzdata |
| 42 | + |
| 43 | +WORKDIR /app |
| 44 | + |
| 45 | +# Copy the instrumented binary |
| 46 | +COPY --from=app-builder /app/client . |
| 47 | + |
| 48 | +# Create non-root user |
| 49 | +RUN addgroup -g 1000 appuser && \ |
| 50 | + adduser -D -u 1000 -G appuser appuser && \ |
| 51 | + chown -R appuser:appuser /app |
| 52 | + |
| 53 | +USER appuser |
| 54 | + |
| 55 | +# Environment variables for OpenTelemetry |
| 56 | +ENV OTEL_EXPORTER_OTLP_ENDPOINT=http://otel-collector:4317 \ |
| 57 | + OTEL_EXPORTER_OTLP_PROTOCOL=grpc \ |
| 58 | + OTEL_SERVICE_NAME=http-client \ |
| 59 | + OTEL_RESOURCE_ATTRIBUTES="service.namespace=demo,service.version=1.0.0" \ |
| 60 | + OTEL_LOG_LEVEL=info |
| 61 | + |
| 62 | +ENTRYPOINT ["./client"] |
| 63 | +CMD ["-addr", "http://http-server:8080"] |
0 commit comments