From 699d6739694873f85386668f9d9d5b325b596576 Mon Sep 17 00:00:00 2001 From: Tomi Hakala Date: Sun, 3 Mar 2024 16:33:20 +0200 Subject: [PATCH] feat(docker): added support for multiplatform build --- Dockerfile | 43 +++++++++++++++++++++++++++++++++---------- Makefile | 22 ++++++++++++++++++++++ 2 files changed, 55 insertions(+), 10 deletions(-) diff --git a/Dockerfile b/Dockerfile index bfc1f5e1..307c8ebb 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,11 +1,24 @@ -FROM golang:1.22.0-bookworm as build - +# Use ARGs to define default build-time variables for TensorFlow version and target platform ARG TENSORFLOW_VERSION=v2.14.0 -ARG PLATFORM=linux_amd64 +# Use TARGETPLATFORM arg provided by buildx to dynamically set platform +#ARG TARGETPLATFORM + +FROM golang:1.22.0-bookworm as build -# Download and configure precompiled TensorFlow Lite C library -RUN curl -L \ - https://github.com/tphakala/tflite_c/releases/download/${TENSORFLOW_VERSION}/tflite_c_${TENSORFLOW_VERSION}_${PLATFORM}.tar.gz | \ +# Pass in ARGs after FROM to use them in build stage +ARG TENSORFLOW_VERSION +ARG TARGETPLATFORM + +# Determine PLATFORM based on TARGETPLATFORM +RUN PLATFORM='unknown'; \ + case "${TARGETPLATFORM}" in \ + "linux/amd64") PLATFORM='linux_amd64' ;; \ + "linux/arm64") PLATFORM='linux_arm64' ;; \ + *) echo "Unsupported platform: '${TARGETPLATFORM}'" && exit 1 ;; \ + esac; \ + # Download and configure precompiled TensorFlow Lite C library for the determined platform + curl -L \ + "https://github.com/tphakala/tflite_c/releases/download/${TENSORFLOW_VERSION}/tflite_c_${TENSORFLOW_VERSION}_${PLATFORM}.tar.gz" | \ tar -C "/usr/local/lib" -xz \ && ldconfig @@ -14,14 +27,21 @@ WORKDIR /root/src # Download TensorFlow headers RUN git clone --branch ${TENSORFLOW_VERSION} --depth 1 https://github.com/tensorflow/tensorflow.git -# Compile BirdNET-GO +# Compile BirdNET-Go COPY . BirdNET-Go -RUN cd BirdNET-Go && make +RUN cd BirdNET-Go && make TARGETPLATFORM=${TARGETPLATFORM} -# Create final image +# Create final image using a multi-platform base image FROM debian:bookworm-slim + +# Install ALSA library and SOX +RUN apt-get update && apt-get install -y \ + libasound2 \ + sox \ + && rm -rf /var/lib/apt/lists/* + COPY --from=build /root/src/BirdNET-Go/bin /usr/bin/ -COPY --from=build /usr/local/lib/libtensorflowlite_c.so /usr/local/lib/libtensorflowlite_c.so +COPY --from=build /usr/local/lib/libtensorflowlite_c.so /usr/local/lib/ RUN ldconfig # Add symlink to /config directory where configs can be stored @@ -31,5 +51,8 @@ RUN mkdir -p /root/.config && ln -s /config /root/.config/birdnet-go VOLUME /data WORKDIR /data +# Make port 8080 available to the world outside this container +EXPOSE 8080 + ENTRYPOINT ["/usr/bin/birdnet-go"] CMD ["realtime"] diff --git a/Makefile b/Makefile index ec93b511..e6a958f5 100644 --- a/Makefile +++ b/Makefile @@ -1,19 +1,41 @@ BINARY_DIR := bin BINARY_NAME := birdnet-go +# Extract GOOS and GOARCH from TARGETPLATFORM +ifeq ($(TARGETPLATFORM),linux/amd64) + GOOS := linux + GOARCH := amd64 +endif +ifeq ($(TARGETPLATFORM),linux/arm64) + GOOS := linux + GOARCH := arm64 +endif + # Common flags CGO_FLAGS := CGO_ENABLED=1 CGO_CFLAGS="-I$(HOME)/src/tensorflow -DMA_NO_PULSEAUDIO" LDFLAGS := -ldflags "-s -w" +# Default build for local development build: $(CGO_FLAGS) go build $(LDFLAGS) -o $(BINARY_DIR)/$(BINARY_NAME) +# Build for Linux amd64 +linux_amd64: + GOOS=linux GOARCH=amd64 $(CGO_FLAGS) go build $(LDFLAGS) -o $(BINARY_DIR)/$(BINARY_NAME) + +# Build for Linux arm64 +linux_arm64: + GOOS=linux GOARCH=arm64 $(CGO_FLAGS) go build $(LDFLAGS) -o $(BINARY_DIR)/$(BINARY_NAME) + +# Windows build windows: GOOS=windows GOARCH=amd64 $(CGO_FLAGS) CC=x86_64-w64-mingw32-gcc go build $(LDFLAGS) -o $(BINARY_DIR)/$(BINARY_NAME).exe +# macOS Intel build macos_intel: GOOS=darwin GOARCH=amd64 $(CGO_FLAGS) go build $(LDFLAGS) -o $(BINARY_DIR)/$(BINARY_NAME) +# macOS ARM build macos_arm: GOOS=darwin GOARCH=arm64 $(CGO_FLAGS) go build $(LDFLAGS) -o $(BINARY_DIR)/$(BINARY_NAME)