From f1e63b6710c5de20a1ec4f6968b3e3d993550510 Mon Sep 17 00:00:00 2001 From: Ed Ram <114068510+edramaspk@users.noreply.github.com> Date: Tue, 17 Oct 2023 15:35:09 +0300 Subject: [PATCH] APPS-934 Add support for multi-platform Docker build using Bake --- Dockerfile | 2 +- README.md | 3 ++- build.sh | 44 ++++++++++++++++++++++++++++++++++++++++++++ docker-bake.hcl | 44 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 91 insertions(+), 2 deletions(-) create mode 100755 build.sh create mode 100644 docker-bake.hcl diff --git a/Dockerfile b/Dockerfile index 734161b5..709b2e66 100644 --- a/Dockerfile +++ b/Dockerfile @@ -30,7 +30,7 @@ RUN make shared EVENT_LIB=libuv RUN ../../scripts/copy_shared.sh ARG GOOS linux -ARG GOARCH amd64 +ARG GOARCH=$BUILDARCH WORKDIR /app/cmd/backup RUN go mod download diff --git a/README.md b/README.md index d87b8b9d..7e00910b 100644 --- a/README.md +++ b/README.md @@ -56,8 +56,9 @@ CGO_CFLAGS="-I/app/modules/aerospike-tools-backup/modules/c-client/target/Linux- It's therefore recommended to always set CGO_ENABLED=0 or CGO_ENABLED=1 when cross-compiling depending on whether you need to use CGO or not. ### Build Docker image +#### AMD64 ``` -docker build -t backup-service . +docker build --build-arg GOARCH=amd64 -t backup-service . ``` #### ARM64 diff --git a/build.sh b/build.sh new file mode 100755 index 00000000..9d7f6baf --- /dev/null +++ b/build.sh @@ -0,0 +1,44 @@ +#!/bin/bash + +BUILDER_NAME="aerospike-builder" +TAG_LATEST=false +TAG="" +PLATFORMS="linux/amd64,linux/arm64" + +POSITIONAL_ARGS=() + +while [[ $# -gt 0 ]]; do + case $1 in + --tag) + TAG="$2" + shift + shift + ;; + --tag-latest) + TAG_LATEST=true + shift + ;; + --platforms) + PLATFORMS="$2" + shift + shift + ;; + -* | --*) + echo "Unknown option $1" + exit 1 + ;; + *) + POSITIONAL_ARGS+=("$1") # save positional arg + shift # past argument + ;; + esac +done + +set -- "${POSITIONAL_ARGS[@]}" + +docker buildx create --name "$BUILDER_NAME" --use + +docker login aerospike.jfrog.io -u "$DOCKER_USERNAME" -p "$DOCKER_PASSWORD" +PLATFORMS="$PLATFORMS" TAG="$TAG" LATEST="$TAG_LATEST" docker buildx bake --no-cache --file docker-bake.hcl + +docker buildx rm "$BUILDER_NAME" diff --git a/docker-bake.hcl b/docker-bake.hcl new file mode 100644 index 00000000..465809ae --- /dev/null +++ b/docker-bake.hcl @@ -0,0 +1,44 @@ +group "default" { + targets = [ + "backup-service" + ] +} + +variable LATEST { + default = false +} + +variable TAG { + default = "" +} + +variable "HUB" { + default = "aerospike.jfrog.io/ecosystem-container-dev-local" +} + +variable "PREFIX" { + default = "aerospike" +} + +variable SETTINGS { + default = "" +} + +variable PLATFORMS { + default = "linux/amd64,linux/arm64" +} + +function "tags" { + params = [service] + result = LATEST == true ? [ + "${HUB}/${PREFIX}-${service}:${TAG}", + "${HUB}/${PREFIX}-${service}:latest"] : ["${HUB}/${PREFIX}-${service}:${TAG}"] +} + +target "backup-service" { + dockerfile = "./Dockerfile" + platforms = split(",", "${PLATFORMS}") + tags = tags("backup-service") + + output = ["type=image,push=true"] +}