Skip to content

Commit

Permalink
build multi-arch Docker image for linux/amd64 and linux/arm64
Browse files Browse the repository at this point in the history
Adds multi-arch Docker image support, and defaults to building
for linux/amd64 and linux/arm64. Uses the docker buildx plugin
along with go cross-compilation.

Closes projectcontour#2868

Signed-off-by: Steve Kriss <[email protected]>
  • Loading branch information
skriss committed Oct 19, 2020
1 parent d2bb321 commit 3a4a373
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 7 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/build_main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
with:
version: latest
- name: Build and Push to registry
env:
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
Expand All @@ -19,5 +23,5 @@ jobs:
TAG_LATEST: "false"
run: |
echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin
make push
make multiarch-build-push
docker logout
4 changes: 4 additions & 0 deletions .github/workflows/build_tag.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
with:
version: latest
- name: Build and Push to registry
env:
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
Expand Down
13 changes: 9 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
FROM golang:1.15.2 AS build
ARG BUILDPLATFORM=linux/amd64

FROM --platform=$BUILDPLATFORM golang:1.15.2 AS build
WORKDIR /contour

ENV GOPROXY=https://proxy.golang.org
Expand All @@ -13,13 +15,16 @@ COPY Makefile Makefile
ARG BUILD_BRANCH
ARG BUILD_SHA
ARG BUILD_VERSION
ARG TARGETOS
ARG TARGETARCH

RUN make install \
RUN make build \
CGO_ENABLED=0 \
GOOS=linux \
GOOS=${TARGETOS} \
GOARCH=${TARGETARCH} \
BUILD_VERSION=${BUILD_VERSION} \
BUILD_SHA=${BUILD_SHA} \
BUILD_BRANCH=${BUILD_BRANCH}

FROM scratch AS final
COPY --from=build /go/bin/contour /bin/contour
COPY --from=build /contour/contour /bin/contour
27 changes: 27 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@ JEKYLL_PORT := 4000
JEKYLL_LIVERELOAD_PORT := 35729

TAG_LATEST ?= false

ifeq ($(TAG_LATEST), true)
IMAGE_TAGS = \
--tag $(IMAGE):$(VERSION) \
--tag $(IMAGE):latest
else
IMAGE_TAGS = \
--tag $(IMAGE):$(VERSION)
endif

# Platforms to build the multi-arch image for.
IMAGE_PLATFORMS ?= linux/amd64,linux/arm64

# Used to supply a local Envoy docker container an IP to connect to that is running
# 'contour serve'. On MacOS this will work, but may not on other OSes. Defining
# LOCALIP as an env var before running 'make local' will solve that.
Expand Down Expand Up @@ -71,6 +84,9 @@ check: install check-test check-test-race ## Install and run tests
.PHONY: checkall
checkall: vendor check lint check-generate

build: ## Build the contour binary
go build -mod=readonly -v -ldflags="$(GO_LDFLAGS)" $(GO_TAGS) $(MODULE)/cmd/contour

install: ## Build and install the contour binary
go install -mod=readonly -v -ldflags="$(GO_LDFLAGS)" $(GO_TAGS) $(MODULE)/cmd/contour

Expand All @@ -84,6 +100,17 @@ download: ## Download Go modules
vendor:
go mod vendor

multiarch-build-push: ## Build and push a multi-arch Contour container image to the Docker registry
docker buildx build \
--platform $(IMAGE_PLATFORMS) \
--build-arg "BUILD_VERSION=$(BUILD_VERSION)" \
--build-arg "BUILD_BRANCH=$(BUILD_BRANCH)" \
--build-arg "BUILD_SHA=$(BUILD_SHA)" \
$(DOCKER_BUILD_LABELS) \
$(IMAGE_TAGS) \
--push \
.

container: ## Build the Contour container image
docker build \
--build-arg "BUILD_VERSION=$(BUILD_VERSION)" \
Expand Down
4 changes: 2 additions & 2 deletions hack/actions/build-and-push-release-images.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ set -o pipefail

# This scripts sets TAG_LATEST based on whether the tag currently being
# built is the highest semantic version tag that's not a pre-release
# (alpha, beta, rc), and calls 'make push'.
# (alpha, beta, rc), and calls 'make multiarch-build-push'.
#
# This script is intended to be run only for tag builds and will no-op
# if GITHUB_REF is not in the format "refs/tags/<tag-name>".
Expand Down Expand Up @@ -54,4 +54,4 @@ echo "CURRENT_TAG: $CURRENT_TAG"
echo "HIGHEST_SEMVER_TAG: $HIGHEST_SEMVER_TAG"
echo "TAG_LATEST: $TAG_LATEST"

make push TAG_LATEST="$TAG_LATEST"
make multiarch-build-push TAG_LATEST="$TAG_LATEST"

0 comments on commit 3a4a373

Please sign in to comment.