Skip to content

Commit d58a89e

Browse files
authored
Merge pull request #1061 from CecileRobertMichon/image-buildx
🔧 Update Docker image build
2 parents 99a0489 + 0e65fc3 commit d58a89e

File tree

5 files changed

+46
-18
lines changed

5 files changed

+46
-18
lines changed

Dockerfile

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# syntax=docker/dockerfile:1.1-experimental
2+
13
# Copyright 2019 The Kubernetes Authors.
24
#
35
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -13,7 +15,7 @@
1315
# limitations under the License.
1416

1517
# Build the manager binary
16-
FROM golang:1.13.15 as builder
18+
FROM golang:1.15.3 as builder
1719
WORKDIR /workspace
1820

1921
# Run this with docker build --build_arg $(go env GOPROXY) to override the goproxy
@@ -23,20 +25,33 @@ ENV GOPROXY=$goproxy
2325
# Copy the Go Modules manifests
2426
COPY go.mod go.mod
2527
COPY go.sum go.sum
28+
2629
# Cache deps before building and copying source so that we don't need to re-download as much
2730
# and so that source changes don't invalidate our downloaded layer
28-
RUN go mod download
31+
RUN --mount=type=cache,target=/go/pkg/mod \
32+
go mod download
2933

3034
# Copy the sources
3135
COPY ./ ./
3236

37+
# Cache the go build into the the Go’s compiler cache folder so we take benefits of compiler caching across docker build calls
38+
RUN --mount=type=cache,target=/root/.cache/go-build \
39+
--mount=type=cache,target=/go/pkg/mod \
40+
go build .
41+
3342
# Build
43+
ARG package=.
3444
ARG ARCH
35-
RUN CGO_ENABLED=0 GOOS=linux GOARCH=${ARCH} \
36-
go build -a -ldflags '-extldflags "-static"' \
37-
-o manager .
45+
ARG ldflags
46+
47+
# Do not force rebuild of up-to-date packages (do not use -a) and use the compiler cache folder
48+
RUN --mount=type=cache,target=/root/.cache/go-build \
49+
--mount=type=cache,target=/go/pkg/mod \
50+
CGO_ENABLED=0 GOOS=linux GOARCH=${ARCH} \
51+
go build -ldflags "${ldflags} -extldflags '-static'" \
52+
-o manager ${package}
3853

39-
# Copy the controller-manager into a thin image
54+
# Production image
4055
FROM gcr.io/distroless/static:latest
4156
WORKDIR /
4257
COPY --from=builder /workspace/manager .

Makefile

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ export GOPROXY
3232
# Active module mode, as we use go modules to manage dependencies
3333
export GO111MODULE=on
3434

35+
# This option is for running docker manifest command
36+
export DOCKER_CLI_EXPERIMENTAL := enabled
37+
3538
# Directories.
3639
ROOT_DIR:=$(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))
3740
TOOLS_DIR := hack/tools
@@ -308,11 +311,17 @@ generate-flavors: $(KUSTOMIZE)
308311
## Docker
309312
## --------------------------------------
310313

314+
.PHONY: docker-pull-prerequisites
315+
docker-pull-prerequisites:
316+
docker pull docker/dockerfile:1.1-experimental
317+
docker pull docker.io/library/golang:1.15.3
318+
docker pull gcr.io/distroless/static:latest
319+
311320
.PHONY: docker-build
312-
docker-build: ## Build the docker image for controller-manager
313-
docker build --pull --build-arg ARCH=$(ARCH) . -t $(CONTROLLER_IMG)-$(ARCH):$(TAG)
314-
MANIFEST_IMG=$(CONTROLLER_IMG)-$(ARCH) MANIFEST_TAG=$(TAG) $(MAKE) set-manifest-image
315-
$(MAKE) set-manifest-pull-policy
321+
docker-build: docker-pull-prerequisites ## Build the docker image for controller-manager
322+
DOCKER_BUILDKIT=1 docker build --build-arg goproxy=$(GOPROXY) --build-arg ARCH=$(ARCH) --build-arg ldflags="$(LDFLAGS)" . -t $(CONTROLLER_IMG)-$(ARCH):$(TAG)
323+
$(MAKE) set-manifest-image MANIFEST_IMG=$(CONTROLLER_IMG)-$(ARCH) MANIFEST_TAG=$(TAG) TARGET_RESOURCE="./config/manager/manager_image_patch.yaml"
324+
$(MAKE) set-manifest-pull-policy TARGET_RESOURCE="./config/manager/manager_pull_policy.yaml"
316325

317326
.PHONY: docker-push
318327
docker-push: ## Push the docker image
@@ -397,7 +406,7 @@ release-binary: $(RELEASE_DIR)
397406
-e GOARCH=$(GOARCH) \
398407
-v "$$(pwd):/workspace" \
399408
-w /workspace \
400-
golang:1.13.15 \
409+
golang:1.15.3 \
401410
go build -a -ldflags '$(LDFLAGS) -extldflags "-static"' \
402411
-o $(RELEASE_DIR)/$(notdir $(RELEASE_BINARY))-$(GOOS)-$(GOARCH) $(RELEASE_BINARY)
403412

Tiltfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def validate_auth():
114114

115115
tilt_helper_dockerfile_header = """
116116
# Tilt image
117-
FROM golang:1.13.15 as tilt-helper
117+
FROM golang:1.15.3 as tilt-helper
118118
# Support live reloading with Tilt
119119
RUN wget --output-document /restart.sh --quiet https://raw.githubusercontent.com/windmilleng/rerun-process-wrapper/master/restart.sh && \
120120
wget --output-document /start.sh --quiet https://raw.githubusercontent.com/windmilleng/rerun-process-wrapper/master/start.sh && \

cloudbuild.yaml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
# See https://cloud.google.com/cloud-build/docs/build-config
2-
timeout: 1200s
2+
timeout: 2700s
33
options:
44
substitution_option: ALLOW_LOOSE
5+
machineType: 'N1_HIGHCPU_8'
56
steps:
6-
- name: 'gcr.io/k8s-testimages/gcb-docker-gcloud:v20190906-745fed4'
7+
- name: 'gcr.io/k8s-testimages/gcb-docker-gcloud:v20200619-68869a4'
78
entrypoint: make
89
env:
9-
- DOCKER_CLI_EXPERIMENTAL=enabled
10-
- TAG=$_GIT_TAG
11-
- PULL_BASE_REF=$_PULL_BASE_REF
10+
- DOCKER_CLI_EXPERIMENTAL=enabled
11+
- TAG=$_GIT_TAG
12+
- PULL_BASE_REF=$_PULL_BASE_REF
13+
- DOCKER_BUILDKIT=1
1214
args:
13-
- release-staging
15+
- release-staging
1416
substitutions:
1517
# _GIT_TAG will be filled with a git-based tag for the image, of the form vYYYYMMDD-hash, and
1618
# can be used as a substitution

hack/boilerplate/boilerplate.Dockerfile.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# syntax=docker/dockerfile:1.1-experimental
2+
13
# Copyright YEAR The Kubernetes Authors.
24
#
35
# Licensed under the Apache License, Version 2.0 (the "License");

0 commit comments

Comments
 (0)