Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix & improve docker multi platform builds #176

Merged
merged 1 commit into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions .github/workflows/docker-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,6 @@ jobs:
needs: test-docker-image
if: github.event_name == 'push' && github.ref_name == 'main'
runs-on: ubuntu-20.04
strategy:
matrix:
include:
- platform: linux/arm64
- platform: linux/amd64
steps:
- name: Checkout GitHub Action
uses: actions/checkout@v4.1.5
Expand All @@ -78,6 +73,7 @@ jobs:
tags: |
ghcr.io/${{ env.REPO }}:dev
ghcr.io/${{ env.REPO }}:${{ github.sha }}
platforms: ${{ matrix.platform }}
cache-from: type=gha,scope=${{ matrix.platform }}
cache-to: type=gha,mode=max,scope=${{ matrix.platform }}
platforms: linux/arm64, linux/amd64
cache-from: type=gha
cache-to: type=gha,mode=max
provenance: false
29 changes: 15 additions & 14 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
# Use ARGs to define default build-time variables for TensorFlow version and target platform
ARG TENSORFLOW_VERSION=v2.14.0

FROM golang:1.22.3-bookworm as buildenv

# Pass in ARGs after FROM to use them in build stage
ARG TENSORFLOW_VERSION
ARG TARGETPLATFORM
FROM --platform=$BUILDPLATFORM golang:1.22.3-bookworm as buildenv

# Install zip utility along with other dependencies
RUN apt-get update && apt-get install -y \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use --no-install-recommends with apt-get install to avoid installing unnecessary packages.

- apt-get install -y curl git sudo zip
+ apt-get install -y --no-install-recommends curl git sudo zip

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
RUN apt-get update && apt-get install -y \
RUN apt-get update && apt-get install -y --no-install-recommends \

Pin versions of packages in apt-get install to ensure consistent builds.

- apt-get install -y curl git sudo zip
+ apt-get install -y curl=7.74.0-1.3+b1 git=1:2.30.2-1 sudo=1.9.5p2-3 zip=3.0-12

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
RUN apt-get update && apt-get install -y \
RUN apt-get update && apt-get install -y curl=7.74.0-1.3+b1 git=1:2.30.2-1 sudo=1.9.5p2-3 zip=3.0-12

Expand All @@ -15,6 +11,18 @@ RUN apt-get update && apt-get install -y \
zip \
&& rm -rf /var/lib/apt/lists/*

WORKDIR /root/src

ARG TENSORFLOW_VERSION

# Download TensorFlow headers
RUN git clone --branch ${TENSORFLOW_VERSION} --filter=blob:none --depth 1 --no-checkout https://github.com/tensorflow/tensorflow.git \
&& git -C tensorflow config core.sparseCheckout true \
&& echo "**/*.h" >> tensorflow/.git/info/sparse-checkout \
&& git -C tensorflow checkout

ARG TARGETPLATFORM

# Determine PLATFORM based on TARGETPLATFORM
RUN PLATFORM='unknown'; \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Set the SHELL option -o pipefail before using pipes in RUN commands to ensure that errors in a pipeline are not masked.

+ SHELL ["/bin/bash", "-o", "pipefail"]
  RUN curl -L ...

Committable suggestion was skipped due low confidence.

case "${TARGETPLATFORM}" in \
Expand All @@ -28,18 +36,11 @@ RUN PLATFORM='unknown'; \
tar -C "/usr/local/lib" -xz \
&& ldconfig

WORKDIR /root/src

# Download TensorFlow headers
RUN git clone --branch ${TENSORFLOW_VERSION} --filter=blob:none --depth 1 --no-checkout https://github.com/tensorflow/tensorflow.git \
&& git -C tensorflow config core.sparseCheckout true \
&& echo "**/*.h" >> tensorflow/.git/info/sparse-checkout \
&& git -C tensorflow checkout

FROM buildenv as build
FROM --platform=$BUILDPLATFORM buildenv as build

# Compile BirdNET-Go
COPY . BirdNET-Go
ARG TARGETPLATFORM
RUN --mount=type=cache,target=/go/pkg/mod \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use WORKDIR instead of cd for changing directories in Dockerfile to follow best practices.

- RUN --mount=type=cache,target=/go/pkg/mod --mount=type=cache,target=/root/.cache/go-build cd BirdNET-Go && make TARGETPLATFORM=${TARGETPLATFORM}
+ WORKDIR /BirdNET-Go
+ RUN --mount=type=cache,target=/go/pkg/mod --mount=type=cache,target=/root/.cache/go-build make TARGETPLATFORM=${TARGETPLATFORM}

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
RUN --mount=type=cache,target=/go/pkg/mod \
WORKDIR /BirdNET-Go
RUN --mount=type=cache,target=/go/pkg/mod --mount=type=cache,target=/root/.cache/go-build make TARGETPLATFORM=${TARGETPLATFORM}

--mount=type=cache,target=/root/.cache/go-build \
cd BirdNET-Go && make TARGETPLATFORM=${TARGETPLATFORM}
Expand Down
Loading