github.com/ava-labs/avalanchego@v1.11.11/Dockerfile (about) 1 # The version is supplied as a build argument rather than hard-coded 2 # to minimize the cost of version changes. 3 ARG GO_VERSION 4 5 # ============= Compilation Stage ================ 6 # Always use the native platform to ensure fast builds 7 FROM --platform=$BUILDPLATFORM golang:$GO_VERSION-bullseye AS builder 8 9 WORKDIR /build 10 11 ARG TARGETPLATFORM 12 ARG BUILDPLATFORM 13 14 # Configure a cross-compiler if the target platform differs from the build platform. 15 # 16 # build_env.sh is used to capture the environmental changes required by the build step since RUN 17 # environment state is not otherwise persistent. 18 RUN if [ "$TARGETPLATFORM" = "linux/arm64" ] && [ "$BUILDPLATFORM" != "linux/arm64" ]; then \ 19 apt-get update && apt-get install -y gcc-aarch64-linux-gnu && \ 20 echo "export CC=aarch64-linux-gnu-gcc" > ./build_env.sh \ 21 ; elif [ "$TARGETPLATFORM" = "linux/amd64" ] && [ "$BUILDPLATFORM" != "linux/amd64" ]; then \ 22 apt-get update && apt-get install -y gcc-x86-64-linux-gnu && \ 23 echo "export CC=x86_64-linux-gnu-gcc" > ./build_env.sh \ 24 ; else \ 25 echo "export CC=gcc" > ./build_env.sh \ 26 ; fi 27 28 # Copy and download avalanche dependencies using go mod 29 COPY go.mod . 30 COPY go.sum . 31 RUN go mod download 32 33 # Copy the code into the container 34 COPY . . 35 36 # Ensure pre-existing builds are not available for inclusion in the final image 37 RUN [ -d ./build ] && rm -rf ./build/* || true 38 39 # Build avalanchego. The build environment is configured with build_env.sh from the step 40 # enabling cross-compilation. 41 ARG RACE_FLAG="" 42 RUN . ./build_env.sh && \ 43 echo "{CC=$CC, TARGETPLATFORM=$TARGETPLATFORM, BUILDPLATFORM=$BUILDPLATFORM}" && \ 44 export GOARCH=$(echo ${TARGETPLATFORM} | cut -d / -f2) && \ 45 ./scripts/build.sh ${RACE_FLAG} 46 47 # Create this directory in the builder to avoid requiring anything to be executed in the 48 # potentially emulated execution container. 49 RUN mkdir -p /avalanchego/build 50 51 # ============= Cleanup Stage ================ 52 # Commands executed in this stage may be emulated (i.e. very slow) if TARGETPLATFORM and 53 # BUILDPLATFORM have different arches. 54 FROM debian:11-slim AS execution 55 56 # Maintain compatibility with previous images 57 COPY --from=builder /avalanchego/build /avalanchego/build 58 WORKDIR /avalanchego/build 59 60 # Copy the executables into the container 61 COPY --from=builder /build/build/ . 62 63 CMD [ "./avalanchego" ]