Skip to content

Commit

Permalink
Enable multi-arch docker images
Browse files Browse the repository at this point in the history
  • Loading branch information
marun committed Apr 5, 2024
1 parent 0eea0bd commit 67391f3
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 15 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/publish_docker_image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install qemu (required for cross-platform builds)
run: |
sudo apt update
sudo apt -y install qemu qemu-user-static
- name: Publish image to DockerHub
env:
DOCKER_USERNAME: ${{ secrets.docker_username }}
Expand Down
40 changes: 34 additions & 6 deletions .github/workflows/publish_image.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,42 @@ source "$AVALANCHE_PATH"/scripts/constants.sh
# Build current avalanchego
source "$AVALANCHE_PATH"/scripts/build_image.sh

echo "$DOCKER_PASS" | docker login --username "$DOCKER_USERNAME" --password-stdin

if [[ $current_branch == "master" ]]; then
echo "Tagging current avalanchego image as $avalanchego_dockerhub_repo:latest"
docker tag "$avalanchego_dockerhub_repo:$current_branch" "$avalanchego_dockerhub_repo:latest"
echo "Tagging current avalanchego images as $avalanchego_dockerhub_repo:latest-{amd64,arm64}"
docker tag "$avalanchego_dockerhub_repo:$COMMIT_HASH-amd64" "$avalanchego_dockerhub_repo:latest-amd64"
docker tag "$avalanchego_dockerhub_repo:$COMMIT_HASH-arm64" "$avalanchego_dockerhub_repo:latest-arm64"
fi

echo "Pushing: $avalanchego_dockerhub_repo:$current_branch"

echo "$DOCKER_PASS" | docker login --username "$DOCKER_USERNAME" --password-stdin

## pushing image with tags
docker image push -a "$avalanchego_dockerhub_repo"

create_multi_arch_image() {
local images=$1
local tag=$2
local repo="$avalanchego_dockerhub_repo"

echo "Creating multi-arch Docker Image $repo:$tag"

docker manifest create "$repo:$tag" $images

# Annotate the manifest with the OS and Architecture for each image
for image in $images; do
local arch=$(echo $image | rev | cut -d- -f1 | rev)
docker manifest annotate "$repo:$tag" "$image" --os linux --arch $arch
done

docker manifest push "$repo:$tag"
}

# Create multi-arch images
IMAGES="$avalanchego_dockerhub_repo:$COMMIT_HASH-amd64 $avalanchego_dockerhub_repo:$COMMIT_HASH-arm64"
create_multi_arch_image "$IMAGES" "$COMMIT_HASH"
create_multi_arch_image "$IMAGES" "$current_branch"
if [[ $current_branch == "master" ]]; then
create_multi_arch_image "$IMAGES" "latest"
fi
RACE_DETECTION_IMAGES="$avalanchego_dockerhub_repo:$COMMIT_HASH-race-amd64 $avalanchego_dockerhub_repo:$COMMIT_HASH-race-arm64"
create_multi_arch_image "$RACE_DETECTION_IMAGES" "$COMMIT_HASH-race"
create_multi_arch_image "$RACE_DETECTION_IMAGES" "$current-branch-race"
22 changes: 20 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,19 @@
# README.md
# go.mod
# ============= Compilation Stage ================
FROM golang:1.21.8-bullseye AS builder
# Always use the build platform to ensure fast builds
FROM --platform=$BUILDPLATFORM golang:1.21.8-bullseye AS builder

# Configure a cross-compiler if the target platform differs from the builder platform
RUN if [ "$TARGETPLATFORM" = "linux/arm64" ] && [ "$BUILDPLATFORM" != "linux/arm64" ]; then \
apt-get update && apt-get install -y gcc-aarch64-linux-gnu && \
export CC=aarch64-linux-gnu-gcc \
; elif [ "$TARGETPLATFORM" = "linux/amd64" ] && [ "$BUILDPLATFORM" != "linux/amd64" ]; then \
apt-get update && apt-get install -y gcc-x86-64-linux-gnu && \
export CC=x86_64-linux-gnu-gcc \
; else \
export CC=gcc \
; fi

WORKDIR /build
# Copy and download avalanche dependencies using go mod
Expand All @@ -21,11 +33,17 @@ COPY . .
ARG RACE_FLAG=""
RUN ./scripts/build.sh ${RACE_FLAG}

# Create the build directory in the builder to avoid requiring
# anything to be executed in the (potentially emulated) execution
# container.
RUN mkdir -p /avalanchego/build

# ============= Cleanup Stage ================
# Will be TARGETPLATFORM and very slow if not the same as BUILDPLATFORM
FROM debian:11-slim AS execution

# Maintain compatibility with previous images
RUN mkdir -p /avalanchego/build
COPY --from=builder /avalanchego/build /avalanchego/build
WORKDIR /avalanchego/build

# Copy the executables into the container
Expand Down
33 changes: 26 additions & 7 deletions scripts/build_image.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

set -euo pipefail

# Cross-platform builds require Docker Buildx and QEMU. buildx should
# be enabled by default as of this writing, and qemu can be installed
# on ubuntu as follows:
#
# sudo apt-get install qemu qemu-user-static
#
#
# Reference: https://docs.docker.com/buildx/working-with-buildx/

# Directory above this script
AVALANCHE_PATH=$( cd "$( dirname "${BASH_SOURCE[0]}" )"; cd .. && pwd )

Expand All @@ -15,12 +24,22 @@ fi

# WARNING: this will use the most recent commit even if there are un-committed changes present
full_commit_hash="$(git --git-dir="$AVALANCHE_PATH/.git" rev-parse HEAD)"
commit_hash="${full_commit_hash::8}"
# Export to ensure the variable is available to the caller
export COMMIT_HASH="${full_commit_hash::8}"

# Build images for supported architectures
TARGET_ARCHITECTURES=("amd64" "arm64")
for TARGET_ARCH in "${TARGET_ARCHITECTURES[@]}"; do
# Only linux is supported
PLATFORM="linux/$TARGET_ARCH"

echo "Building Docker Image with tags: $avalanchego_dockerhub_repo:$commit_hash , $avalanchego_dockerhub_repo:$current_branch"
docker build -t "$avalanchego_dockerhub_repo:$commit_hash" \
-t "$avalanchego_dockerhub_repo:$current_branch" "$AVALANCHE_PATH" -f "$AVALANCHE_PATH/Dockerfile"
echo "Building Docker Image with tags: $avalanchego_dockerhub_repo:$COMMIT_HASH-$TARGET_ARCH , $avalanchego_dockerhub_repo:$current_branch-$TARGET_ARCH"
docker buildx build --platform="$PLATFORM" \
-t "$avalanchego_dockerhub_repo:$COMMIT_HASH-$TARGET_ARCH" -t "$avalanchego_dockerhub_repo:$current_branch-$TARGET_ARCH" \
"$AVALANCHE_PATH" --load -f "$AVALANCHE_PATH/Dockerfile"

echo "Building Docker Image with tags: $avalanchego_dockerhub_repo:$commit_hash-race , $avalanchego_dockerhub_repo:$current_branch-race"
docker build --build-arg="RACE_FLAG=-r" -t "$avalanchego_dockerhub_repo:$commit_hash-race" \
-t "$avalanchego_dockerhub_repo:$current_branch-race" "$AVALANCHE_PATH" -f "$AVALANCHE_PATH/Dockerfile"
echo "Building Docker Image with tags: $avalanchego_dockerhub_repo:$COMMIT_HASH-race-$TARGET_ARCH , $avalanchego_dockerhub_repo:$current_branch-race-$TARGET_ARCH"
docker buildx build --platform="$PLATFORM" --build-arg="RACE_FLAG=-r" \
-t "$avalanchego_dockerhub_repo:$COMMIT_HASH-race-$TARGET_ARCH" -t "$avalanchego_dockerhub_repo:$current_branch-race-$TARGET_ARCH" \
"$AVALANCHE_PATH" --load -f "$AVALANCHE_PATH/Dockerfile"
done

0 comments on commit 67391f3

Please sign in to comment.