Skip to content

Commit

Permalink
Faster image builds with caching (blakeblackshear#4491)
Browse files Browse the repository at this point in the history
* fix makefile variable

* add branch for testing

* fix arm32 build

* use amd64 for web build

* install wheels in a separate layer for better parallel builds

* try build-push-action

* try using gh context

* use short sha

* cleanup
  • Loading branch information
blakeblackshear committed Nov 24, 2022
1 parent f4187d6 commit 370276a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 21 deletions.
16 changes: 12 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,15 @@ jobs:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build image
run: make push
env:
IMAGE_REPO: ghcr.io/${{ github.actor }}/frigate
- name: Create short sha
run: echo "SHORT_SHA=${GITHUB_SHA::7}" >> $GITHUB_ENV
- name: Build and push
uses: docker/build-push-action@v3
with:
context: .
push: true
platforms: linux/amd64,linux/arm64,linux/arm/v7
tags: |
ghcr.io/blakeblackshear/frigate:${{ github.ref_name }}-${{ env.SHORT_SHA }}
cache-from: type=gha
cache-to: type=gha,mode=max
23 changes: 9 additions & 14 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ RUN --mount=type=bind,source=docker/install_s6_overlay.sh,target=/deps/install_s

FROM base AS wheels
ARG DEBIAN_FRONTEND
ARG TARGETARCH

# Use a separate container to build wheels to prevent build dependencies in final image
RUN apt-get -qq update \
Expand Down Expand Up @@ -106,9 +107,11 @@ ENV PATH="/usr/lib/btbn-ffmpeg/bin:/usr/local/go2rtc/bin:/usr/local/nginx/sbin:$

# Install dependencies
RUN --mount=type=bind,source=docker/install_deps.sh,target=/deps/install_deps.sh \
--mount=type=bind,from=wheels,source=/wheels,target=/deps/wheels \
/deps/install_deps.sh

RUN --mount=type=bind,from=wheels,source=/wheels,target=/deps/wheels \
pip3 install -U /deps/wheels/*.whl

COPY --from=deps-rootfs / /

EXPOSE 5000
Expand All @@ -118,8 +121,8 @@ EXPOSE 8555

ENTRYPOINT ["/init"]

# Frigate deps with Node.js and NPM
FROM deps AS deps-node
# Frigate deps with Node.js and NPM for devcontainer
FROM deps AS devcontainer

# Install Node 16
RUN apt-get update \
Expand All @@ -129,9 +132,6 @@ RUN apt-get update \
&& rm -rf /var/lib/apt/lists/* \
&& npm install -g npm@9

# Devcontainer
FROM deps-node AS devcontainer

WORKDIR /workspace/frigate

RUN apt-get update \
Expand All @@ -145,7 +145,8 @@ CMD ["sleep", "infinity"]


# Frigate web build
FROM deps-node AS web-build
# force this to run on amd64 because QEMU is painfully slow
FROM --platform=linux/amd64 node:16 AS web-build

WORKDIR /work
COPY web/package.json web/package-lock.json ./
Expand All @@ -154,19 +155,13 @@ RUN npm install
COPY web/ ./
RUN npm run build

# Frigate web dist files
FROM scratch AS web-dist

COPY --from=web-build /work/dist/ /

# Collect final files in a single layer
FROM scratch AS rootfs

WORKDIR /opt/frigate/
COPY frigate frigate/
COPY migrations migrations/
COPY --from=web-dist / web/

COPY --from=web-build /work/dist/ web/

# Frigate final container
FROM deps
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ default_target: local

COMMIT_HASH := $(shell git log -1 --pretty=format:"%h"|tail -1)
VERSION = 0.12.0
IMAGE_OWNER ?= ghcr.io/blakeblackshear/frigate
IMAGE_REPO ?= ghcr.io/blakeblackshear/frigate
CURRENT_UID := $(shell id -u)
CURRENT_GID := $(shell id -g)

Expand Down
3 changes: 1 addition & 2 deletions docker/install_deps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ sed -i -e's/ main/ main contrib non-free/g' /etc/apt/sources.list
apt-get -qq update
apt-get -qq install --no-install-recommends --no-install-suggests -y \
libedgetpu1-max python3-tflite-runtime python3-pycoral
pip3 install -U /deps/wheels/*.whl

# btbn-ffmpeg -> amd64 / arm64
if [[ "${TARGETARCH}" == "amd64" || "${TARGETARCH}" == "arm64" ]]; then
Expand Down Expand Up @@ -69,7 +68,7 @@ if [[ "${TARGETARCH}" == "arm" ]]; then
libgtk-3-dev \
libavcodec-dev libavformat-dev libswscale-dev libv4l-dev \
libxvidcore-dev libx264-dev libjpeg-dev libpng-dev libtiff-dev \
gfortran openexr libatlas-base-dev libssl-devlibtbb2 libtbb-dev libdc1394-22-dev libopenexr-dev \
gfortran openexr libatlas-base-dev libtbb-dev libdc1394-22-dev libopenexr-dev \
libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev
fi

Expand Down

0 comments on commit 370276a

Please sign in to comment.