From 7cb90df7a929aa1e1112b18275320604ab9edf44 Mon Sep 17 00:00:00 2001 From: Shihaam Abdul Rahman Date: Sat, 7 Oct 2023 21:44:47 +0500 Subject: [PATCH] multi arch test --- 10/build-push.sh | 24 +++++++++++ 10/docker-compose.yml | 12 ++++-- 10/readimage.sh | 98 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 130 insertions(+), 4 deletions(-) create mode 100755 10/build-push.sh create mode 100755 10/readimage.sh diff --git a/10/build-push.sh b/10/build-push.sh new file mode 100755 index 0000000..7d2286f --- /dev/null +++ b/10/build-push.sh @@ -0,0 +1,24 @@ +#!/bin/bash + + +docker compose build + +docker tag debian-curl:x86-10 git.shihaam.dev/dockerfiles/debian-curl:x86-10 +docker tag debian-curl:arm-10 git.shihaam.dev/dockerfiles/debian-curl:arm-10 + + +docker push git.shihaam.dev/dockerfiles/debian-curl:x86-10 +docker push git.shihaam.dev/dockerfiles/debian-curl:arm-10 + +docker manifest create git.shihaam.dev/dockerfiles/debian-curl:10 \ + git.shihaam.dev/dockerfiles/debian-curl:x86-10 \ + git.shihaam.dev/dockerfiles/debian-curl:arm-10 + +#docker manifest annotate git.shihaam.dev/dockerfiles/debian-curl:10 \ +#git.shihaam.dev/dockerfiles/debian-curl:x86-10 --os linux --arch amd64 + +#docker manifest annotate git.shihaam.dev/dockerfiles/debian-curl:10 \ +#git.shihaam.dev/dockerfiles/debian-curl:arm-10 --os linux --arch arm64 + + +docker manifest push git.shihaam.dev/dockerfiles/debian-curl:10 diff --git a/10/docker-compose.yml b/10/docker-compose.yml index 03f5306..682f60c 100644 --- a/10/docker-compose.yml +++ b/10/docker-compose.yml @@ -1,12 +1,16 @@ version: '3.5' services: debian10-x86: - build: . + build: + context: . + dockerfile: Dockerfile hostname: debian10-x86 platform: linux/amd64/v3 - image: git.shihaam.dev/dockerfiles/debian-curl:10 + image: debian-curl:x86-10 debian10-arm: - build: . + build: + context: . + dockerfile: Dockerfile hostname: debian10-arm platform: linux/arm64 - image: git.shihaam.dev/dockerfiles/debian-curl:10 + image: debian-curl:arm-10 diff --git a/10/readimage.sh b/10/readimage.sh new file mode 100755 index 0000000..f042b29 --- /dev/null +++ b/10/readimage.sh @@ -0,0 +1,98 @@ +#!/bin/bash + +# Check if podman is installed +if command -v podman &> /dev/null +then + container_engine="podman" +# Check if docker is installed +elif command -v docker &> /dev/null +then + container_engine="docker" +else + # Neither podman nor docker is installed + echo "Neither Podman nor Docker is installed." + echo "Please install either Podman or Docker to proceed." + exit 1 +fi +echo "Using $container_engine as the container engine." +# +yq() { + $container_engine run --rm -i -v "${PWD}":/workdir mikefarah/yq "$@" +} + +# Default file +FILE="docker-compose.yml" + +# Check for -f argument +while getopts "f:" opt; do + case $opt in + f) + FILE="$OPTARG" + ;; + *) + echo "Usage: $0 [-f docker-compose-file] build " + exit 1 + ;; + esac +done + +# Check if file exists +if [ ! -f "$FILE" ]; then + echo "File $FILE does not exist." + exit 1 +fi + +# Read and print service name, platform, and image +services=$(yq e '.services | keys | .[]' "$FILE") + +for service in $services; do + platform=$(yq e ".services.$service.platform" "$FILE") + image=$(yq e ".services.$service.image" "$FILE") + echo "Service: $service" + echo "Platform: $platform" + echo "Image: $image" + echo "---------------------" +done + +build_image() { + local service=$1 + local platform=$(yq e ".services.$service.platform" "$FILE") + local image=$(yq e ".services.$service.image" "$FILE") + local context=$(yq e ".services.$service.build.context" "$FILE") + local dockerfile=$(yq e ".services.$service.build.dockerfile" "$FILE") + + # Check if platform is defined + if [ -z "$platform" ]; then + echo "Error: Platform is not defined for service $service." + exit 1 + fi + + # Replace '/' and ':' with '-' in the platform string to make it suitable for a tag + local platform_tag=$(echo "$platform" | tr '/:' '-') + + # Append platform information to the image tag + local new_image_tag="${image}-${platform_tag}" + + echo "Building service: $service" + echo "Platform: $platform" + echo "Image: $new_image_tag" + echo "---------------------" + + $container_engine buildx build --platform "$platform" -t "$new_image_tag" -f "$context/$dockerfile" "$context" +} +# Check for "build" argument and optional service name +if [ "$1" == "build" ]; then + if [ "$#" -ge 2 ]; then + # Build specified service + build_image "$2" + else + # Build all services + services=$(yq e '.services | keys | .[]' "$FILE") + for service in $services; do + build_image "$service" + done + fi +else + echo "Usage: $0 [-f docker-compose-file] build [service_name]" + exit 1 +fi