Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
marun committed Apr 5, 2024
1 parent 4967749 commit f1199c1
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 23 deletions.
36 changes: 19 additions & 17 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,18 @@
# README.md
# go.mod
# ============= Compilation Stage ================
FROM golang:1.21.8-bullseye AS builder

# Allow the target arch to be configured to support building both amd64 and arm64
ARG TARGETARCH=amd64

# Use a cross-compiler if the target architecture is arm64. The default build environment is amd64.
RUN if [ "${TARGETARCH}" = "arm64" ]; then \
apt-get update && apt-get install -y gcc-aarch64-linux-gnu && \
export TARGETCC=aarch64-linux-gnu-gcc; \
else \
export TARGETCC=gcc; \
fi

ENV GOARCH="${TARGETARCH}"
ENV CC="${TARGETCC}"
FROM --platform=$BUILDPLATFORM golang:1.21.8-bullseye AS builder # Always use the build platform to ensure fast builds

# 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" = "amd64" ] && [ "$BUILDPLATFORM" != "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 @@ -35,11 +32,16 @@ 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 ================
FROM debian:11-slim AS execution
FROM debian:11-slim AS execution # Will be TARGETPLATFORM and very slow if not the same as BUILDPLATFORM

# 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
47 changes: 41 additions & 6 deletions scripts/build_image.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,45 @@ fi
full_commit_hash="$(git --git-dir="$AVALANCHE_PATH/.git" rev-parse HEAD)"
commit_hash="${full_commit_hash::8}"

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"
# 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-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 for ${PLATFORM} without race detection"
docker buildx build --platform="$PLATFORM" --progress=plain \
-t "$avalanchego_dockerhub_repo:$commit_hash-$TARGET_ARCH" \
"$AVALANCHE_PATH" --load -f "$AVALANCHE_PATH/Dockerfile"

echo "Building Docker Image for ${PLATFORM} with race detection"
docker buildx build --platform="$PLATFORM" --build-arg="RACE_FLAG=-r"
-t "$avalanchego_dockerhub_repo:$commit_hash-race-$TARGET_ARCH" \
"$AVALANCHE_PATH" --load -f "$AVALANCHE_PATH/Dockerfile"
done

IMAGES="avalanchego_dockerhub_repo:$commit_hash-amd64 avalanchego_dockerhub_repo:$commit_hash-arm64"
echo "Creating multi-arch Docker Image $avalanchego_dockerhub_repo:$commit_hash"
docker manifest create "$avalanchego_dockerhub_repo:$commit_hash" $IMAGES
docker manifest annotate "$avalanchego_dockerhub_repo:$commit_hash" "avalanchego_dockerhub_repo:$commit_hash-amd64" --os linux --arch amd64
docker manifest annotate "$avalanchego_dockerhub_repo:$commit_hash" "avalanchego_dockerhub_repo:$commit_hash-arm64" --os linux --arch arm64
echo "Creating multi-arch Docker Image $avalanchego_dockerhub_repo:$current_branch"
docker manifest create "$avalanchego_dockerhub_repo:$current_branch" $IMAGES
docker manifest annotate "$avalanchego_dockerhub_repo:$current_branch" "avalanchego_dockerhub_repo:$current_branch-amd64" --os linux --arch amd64
docker manifest annotate "$avalanchego_dockerhub_repo:$current_branch" "avalanchego_dockerhub_repo:$current_branch-arm64" --os linux --arch arm64
if [[ $current_branch == "master" ]]; then
echo "Creating multi-arch image as $avalanchego_dockerhub_repo:latest"
docker manifest create "$avalanchego_dockerhub_repo:latest" $IMAGES
docker manifest annotate "$avalanchego_dockerhub_repo:$latest" "avalanchego_dockerhub_repo:$current_branch-amd64" --os linux --arch amd64
docker manifest annotate "$avalanchego_dockerhub_repo:$latest" "avalanchego_dockerhub_repo:$current_branch-arm64" --os linux --arch arm64
fi

RACE_DETECTION_IMAGES="avalanchego_dockerhub_repo:$commit_hash-race-amd64 avalanchego_dockerhub_repo:$commit_hash-race-arm64"
echo "Creating multi-arch Docker Image $avalanchego_dockerhub_repo:$commit_hash-race"
docker manifest create "$avalanchego_dockerhub_repo:$commit_hash-race" $RACE_DETECTION_IMAGES
docker manifest annotate "$avalanchego_dockerhub_repo:$commit_hash-race" "avalanchego_dockerhub_repo:$commit_hash-race-amd64" --os linux --arch amd64
docker manifest annotate "$avalanchego_dockerhub_repo:$commit_hash-race" "avalanchego_dockerhub_repo:$commit_hash-race-arm64" --os linux --arch arm64
echo "Creating multi-arch Docker Image $avalanchego_dockerhub_repo:$current_branch-race"
docker manifest create "$avalanchego_dockerhub_repo:$current_branch-race" $RACE_DETECTION_IMAGES
docker manifest annotate "$avalanchego_dockerhub_repo:$current_branch-race" "avalanchego_dockerhub_repo:$current_branch-race-amd64" --os linux --arch amd64
docker manifest annotate "$avalanchego_dockerhub_repo:$current_branch-race" "avalanchego_dockerhub_repo:$current_branch-race-arm64" --os linux --arch arm64

0 comments on commit f1199c1

Please sign in to comment.