Skip to content

Commit b7d417e

Browse files
authored
Merge pull request #728 from dmvolod/build-image-from-private-repo
🌱 Add ability to build operator images in a private docker registry
2 parents 89538b0 + 4bd3be7 commit b7d417e

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

Dockerfile

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
# Build the manager binary
1818
# Run this with docker build --build-arg builder_image=<golang:x.y.z>
1919
ARG builder_image
20+
ARG deployment_base_image
21+
ARG deployment_base_image_tag
22+
ARG goprivate
2023

2124
FROM ${builder_image} as builder
2225
WORKDIR /workspace
@@ -25,14 +28,16 @@ WORKDIR /workspace
2528
ARG goproxy=https://proxy.golang.org
2629
# Run this with docker build --build-arg package=./controlplane/kubeadm or --build-arg package=./bootstrap/kubeadm
2730
ENV GOPROXY=$goproxy
31+
ENV GOPRIVATE=$goprivate
2832

2933
# Copy the Go Modules manifests
3034
COPY go.mod go.mod
3135
COPY go.sum go.sum
3236

3337
# Cache deps before building and copying source so that we don't need to re-download as much
3438
# and so that source changes don't invalidate our downloaded layer
35-
RUN --mount=type=cache,target=/go/pkg/mod \
39+
RUN --mount=type=secret,id=netrc,required=false,target=/root/.netrc \
40+
--mount=type=cache,target=/go/pkg/mod \
3641
go mod download
3742

3843
# Copy the sources
@@ -50,7 +55,7 @@ RUN --mount=type=cache,target=/go/pkg/mod \
5055
-o manager ${path}
5156

5257
# Production image
53-
FROM gcr.io/distroless/static:nonroot
58+
FROM ${deployment_base_image}:${deployment_base_image_tag}
5459
WORKDIR /
5560
COPY --from=builder /workspace/manager .
5661
# Use uid of nonroot user (65532) because kubernetes expects numeric user when applying pod security policies

Makefile

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ ROOT:=$(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))
2424
.DEFAULT_GOAL:=help
2525

2626
GO_VERSION ?= 1.23.0
27-
GO_CONTAINER_IMAGE ?= docker.io/library/golang:$(GO_VERSION)
27+
GO_BASE_CONTAINER ?= docker.io/library/golang
28+
GO_CONTAINER_IMAGE = $(GO_BASE_CONTAINER):$(GO_VERSION)
2829

2930
# Use GOPROXY environment variable if set
3031
GOPROXY := $(shell go env GOPROXY)
@@ -33,9 +34,21 @@ GOPROXY := https://proxy.golang.org
3334
endif
3435
export GOPROXY
3536

37+
# Use GOPRIVATE environment variable if set
38+
GOPRIVATE := $(shell go env GOPRIVATE)
39+
export GOPRIVATE
40+
41+
# Base docker images
42+
43+
DOCKERFILE_CONTAINER_IMAGE ?= docker.io/docker/dockerfile:1.4
44+
DEPLOYMENT_BASE_IMAGE ?= gcr.io/distroless/static
45+
DEPLOYMENT_BASE_IMAGE_TAG ?= nonroot-${ARCH}
46+
3647
# Active module mode, as we use go modules to manage dependencies
3748
export GO111MODULE=on
3849

50+
BUILD_CONTAINER_ADDITIONAL_ARGS ?=
51+
3952
# This option is for running docker manifest command
4053
export DOCKER_CLI_EXPERIMENTAL := enabled
4154

@@ -372,13 +385,13 @@ modules: ## Runs go mod to ensure modules are up to date.
372385

373386
.PHONY: docker-pull-prerequisites
374387
docker-pull-prerequisites:
375-
docker pull docker.io/docker/dockerfile:1.4
388+
docker pull $(DOCKERFILE_CONTAINER_IMAGE)
376389
docker pull $(GO_CONTAINER_IMAGE)
377-
docker pull gcr.io/distroless/static:latest
390+
docker pull $(DEPLOYMENT_BASE_IMAGE):$(DEPLOYMENT_BASE_IMAGE_TAG)
378391

379392
.PHONY: docker-build
380393
docker-build: docker-pull-prerequisites ## Build the docker image for controller-manager
381-
docker build --build-arg builder_image=$(GO_CONTAINER_IMAGE) --build-arg goproxy=$(GOPROXY) --build-arg ARCH=$(ARCH) --build-arg LDFLAGS="$(LDFLAGS)" . -t $(CONTROLLER_IMG_TAG)
394+
docker build $(BUILD_CONTAINER_ADDITIONAL_ARGS) --build-arg builder_image=$(GO_CONTAINER_IMAGE) --build-arg deployment_base_image=$(DEPLOYMENT_BASE_IMAGE) --build-arg deployment_base_image_tag=$(DEPLOYMENT_BASE_IMAGE_TAG) --build-arg goproxy=$(GOPROXY) --build-arg goprivate=$(GOPRIVATE) --build-arg ARCH=$(ARCH) --build-arg LDFLAGS="$(LDFLAGS)" . -t $(CONTROLLER_IMG_TAG)
382395

383396
.PHONY: docker-push
384397
docker-push: ## Push the docker image

0 commit comments

Comments
 (0)