Skip to content

Commit

Permalink
refactor: enhance Dockerfile for improved build automation and user s…
Browse files Browse the repository at this point in the history
…etup

- Added a VERSION argument to the Dockerfile for better version management during builds.
- Streamlined the installation of dependencies by including Taskfile installation directly in the Dockerfile.
- Improved user setup by ensuring the dev-user's home directory structure is created and owned correctly.
- Updated the build process to utilize Task commands for checking TensorFlow, downloading assets, and generating Tailwind CSS, enhancing automation and clarity.
- Adjusted the final image to copy the TensorFlow Lite library from the build stage, ensuring compatibility with the specified architecture.
  • Loading branch information
tphakala committed Jan 22, 2025
1 parent 4870b03 commit 9dde07d
Showing 1 changed file with 22 additions and 25 deletions.
47 changes: 22 additions & 25 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,51 +1,48 @@
ARG TFLITE_LIB_DIR=/usr/lib
ARG TENSORFLOW_VERSION=2.17.1
ARG TARGETPLATFORM=linux/amd64 # Default to linux/amd64 for local builds
ARG VERSION=unknown

FROM --platform=$TARGETPLATFORM golang:1.23.5-bookworm AS buildenv
FROM --platform=$TARGETPLATFORM golang:1.23.5-bookworm AS build

# Install zip utility along with other dependencies
# Install Task and other dependencies
RUN apt-get update && apt-get install -y \
curl \
git \
sudo \
zip \
npm \
gcc-aarch64-linux-gnu \
&& rm -rf /var/lib/apt/lists/*
&& rm -rf /var/lib/apt/lists/* \
&& sh -c "$(curl --location https://taskfile.dev/install.sh)" -- -d -b /usr/local/bin

# Create dev-user for building and devcontainer usage
RUN groupadd --gid 10001 dev-user; \
useradd --uid 10001 --gid dev-user --shell /bin/bash --create-home dev-user; \
usermod -aG sudo dev-user; \
usermod -aG audio dev-user; \
echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
USER dev-user

WORKDIR /home/dev-user/src/BirdNET-Go
RUN groupadd --gid 10001 dev-user && \
useradd --uid 10001 --gid dev-user --shell /bin/bash --create-home dev-user && \
usermod -aG sudo dev-user && \
usermod -aG audio dev-user && \
echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers && \
mkdir -p /home/dev-user/src && \
mkdir -p /home/dev-user/lib && \
chown -R dev-user:dev-user /home/dev-user

COPY --chown=dev-user ./Makefile ./
COPY --chown=dev-user ./reset_auth.sh ./

# Download TensorFlow headers
RUN make check-tensorflow

FROM --platform=$TARGETPLATFORM buildenv AS build
USER dev-user
WORKDIR /home/dev-user/src/BirdNET-Go

# First copy all source files
# Copy all source files first to have Git information available
COPY --chown=dev-user . ./

# Then download assets and generate CSS with the input file available
RUN make download-assets
RUN make generate-tailwindcss
# Now run the tasks with VERSION env var
RUN VERSION=${VERSION} task check-tensorflow && \
VERSION=${VERSION} task download-assets && \
VERSION=${VERSION} task generate-tailwindcss

# Compile BirdNET-Go
ARG TARGETPLATFORM
RUN --mount=type=cache,target=/go/pkg/mod,uid=10001,gid=10001 \
--mount=type=cache,target=/home/dev-user/.cache/go-build,uid=10001,gid=10001 \
TARGET=$(echo ${TARGETPLATFORM} | tr '/' '_') && \
make ${TARGET}
DOCKER_LIB_DIR=/home/dev-user/lib task ${TARGET}

# Create final image using a multi-platform base image
FROM debian:bookworm-slim
Expand All @@ -68,8 +65,8 @@ RUN if [ "$TARGETPLATFORM" = "linux/arm64" ]; then \
fi && \
echo "Using TFLITE_LIB_DIR=$TFLITE_LIB_DIR"

# Use the TFLITE_LIB_DIR for the library copy
COPY --from=build ${TFLITE_LIB_DIR}/libtensorflowlite_c.so ${TFLITE_LIB_DIR}/
# Copy TensorFlow Lite library from build stage
COPY --from=build /home/dev-user/lib/libtensorflowlite_c.so* ${TFLITE_LIB_DIR}/
RUN ldconfig

# Include reset_auth tool from build stage
Expand Down

0 comments on commit 9dde07d

Please sign in to comment.