Dockerfile 1.91 KB
Newer Older
1
# SPDX-FileCopyrightText: Copyright (c) 2024-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
Julien Mancuso's avatar
Julien Mancuso committed
2
3
4
# SPDX-License-Identifier: Apache-2.0

# Base stage - Common setup for all stages
5
6
ARG DOCKER_PROXY
ARG BASE_IMAGE="golang"
7
ARG BASE_IMAGE_TAG="1.25"
8
FROM ${DOCKER_PROXY}${BASE_IMAGE}:${BASE_IMAGE_TAG} AS base
Julien Mancuso's avatar
Julien Mancuso committed
9
10
11

# Docker buildx automatically provides these
ARG TARGETOS=linux
12
ARG TARGETARCH
Julien Mancuso's avatar
Julien Mancuso committed
13
14
15

RUN echo "Building for ${TARGETOS}/${TARGETARCH}"

16
17
18
# Install common dependencies (python3-pip needed for generate-pydantic target)
RUN apt-get update && apt-get install -y --no-install-recommends git python3-pip && apt-get clean && rm -rf /var/lib/apt/lists/* \
    && pip3 install --no-cache-dir pydantic --break-system-packages
Julien Mancuso's avatar
Julien Mancuso committed
19
20
21
22
23

WORKDIR /workspace

# Copy go mod and sum files first for better layer caching
COPY go.mod go.sum ./
24
COPY --from=snapshot go.mod go.sum /snapshot/
Julien Mancuso's avatar
Julien Mancuso committed
25
26
27
28
RUN go mod download

# Copy source code
COPY . .
29
COPY --from=snapshot . /snapshot/
Julien Mancuso's avatar
Julien Mancuso committed
30
31
32
33
34
35
36
37
38
39
40
41
42
43

# Lint stage
FROM base AS linter

# Install golangci-lint using go install (builds with current Go version)
RUN go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.62.2

# Run linting
RUN golangci-lint run --timeout=5m

# Test stage
FROM base AS tester

# Install make if not present
44
RUN apt-get update && apt-get install -y --no-install-recommends make && apt-get clean && rm -rf /var/lib/apt/lists/*
Julien Mancuso's avatar
Julien Mancuso committed
45
46
47
48
49
50
51

# Run tests using Makefile
RUN make test

# Build stage - depends on successful lint and test
FROM base AS builder

52
53
RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -o manager ./cmd/main.go && \
    CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -o crd-apply ./cmd/crd-apply/main.go
Julien Mancuso's avatar
Julien Mancuso committed
54
55

# Runtime stage
56
FROM nvcr.io/nvidia/distroless/go:v4.0.3
Julien Mancuso's avatar
Julien Mancuso committed
57
58
WORKDIR /
COPY --from=builder /workspace/manager .
59
60
COPY --from=builder /workspace/crd-apply .
COPY --from=builder /workspace/config/crd/bases/ /opt/dynamo-operator/crds/
Julien Mancuso's avatar
Julien Mancuso committed
61
62
63
USER 65532:65532

ENTRYPOINT ["./manager"]